UNCLASSIFIED

Skip to content
Snippets Groups Projects
Commit 4ee0ce38 authored by nicholas.r.yost's avatar nicholas.r.yost
Browse files

mv demos into public from private

parent f73abc93
Branches
1 merge request!10mv demos into public from internal
Pipeline #43814 failed with stage
in 4 seconds
Showing
with 391 additions and 0 deletions
# Create variables by assigning values
$total = 150
$diff = 0.25
# Math
$result = $total * $diff
# Output result
$result
# Show variable values in text
$text = "$result is {0:0%} of $total" -f $diff
$text
# Assign multiple variables
$x = $y = $z = 10
$x
$y
$z
# Assign different values to several variables
$var1, $var2 = 50, 100
$var1
$var2
# Create a new hash table
$mylist = @{First = "John"; Last = "Doe"; Mid = "Bon"; Age = 35}
# Access the value for the key "Last"
$mylist["Last"]
$mylist.Last
# Access values from multiple keys
$mylist["First", "Mid", "Last"]
# Return all keys
$mylist.Keys
# Return all Values
$mylist.values
# Output all elements
$mylist[$mylist.keys]
# Store array in hash table
$list = @{ element1 = 5; array1 = 1..5; array2 = 6, 7, 8}
# Adding new keys
$mylist = @{First = "John"; Last = "Doe"; Mid = "Bon"; Age = 35}
$mylist.Date = Get-Date
$mylist["Location"] = "Germany"
$mylist
# Manipulating values
$mylist.Mid = "Kimberly"
$mylist["Location"] = "Oregon"
# Remove value
$mylist.Remove("Date")
\ No newline at end of file
# List current Variables
Get-Variable
Get-ChildItem variable:
# Output specific variable
$x = 10
Get-Variable x
Get-ChildItem variable:x
# Verify a variable using Test-Path
Test-Path variable:x
# Deleting Variables
Remove-Variable x
del variable:x
# List of automatic variables and their description
Get-Variable | Sort-Object Name | Format-Table Name, Description -AutoSize -Wrap
\ No newline at end of file
# Storing results in arrays
$x = Get-Process
# Create non-array
$a = "Hello World"
# Identifying Arrays
$x -is [array]
$a -is [array]
# Find number of elements stored in array
$x.count
\ No newline at end of file
# Create new array
$array = 1,2,3,4
$array
# Show second element
$array[1]
# Shortcut for sequential numbers
$array1 = 1..4
$array1
# Polymorphic Arrays
$array2 = "World", "hello", 5, 10, (Get-Date)
$array2
# Empty Array
$array3 = @()
\ No newline at end of file
# Create Array
$array = -5..10
# Access second element
$array[1]
# Access last element
$array[-1]
$array[$array.Count-1]
$array[$array.Length-1]
# Access generated array not stored in a variable
(-4..10)[4]
\ No newline at end of file
# Create array
$list = Get-Process
# Display only the 2nd, 5th, 9th, & 13th elements
$list[1, 4, 8, 12]
# Jagged Arrays
$jagarray = "joe", "jim", "jan", (1, ('apple', 'pear'), 3), "jay"
# Address first element
$jagarray[0]
# Address array at third element
$jagarray[3]
# Address first element in middle array
$jagarray[3][1][0]
# Multiply Strings
'_' * 20
'BOOM ' * 20
# Create text array using '@()'
@('BOOM') * 20
# Create Array with 20 elements
@(0) * 20
# Shortcut
,0 * 20
# Finding cmdlets
Help format*
Help *content*
Help write*
Help about_*
# List aliases
Get-Alias
Get-ChildItem alias:
# Create alias
Set-alias gh Get-Help
\ No newline at end of file
# Using Get-Unique
1,2,3,1,2,3,1,2,3,1,2,3,1,2,3 | Sort-Object | Get-Unique
# Using Measure-Object
Get-ChildItem | Measure-Object Length
# Using Parameters with Measure-Object
Get-ChildItem | Measure-Object Length -Average -Maximum -Minimum -Sum
# Create test file and snapshot of directory
'What is the answer to life, the universe, and everything?' > test.txt
$before = Get-ChildItem
# Modify test file and new snapshot
'42' > test.txt
$after = Get-ChildItem
# Use Compare-Object to compare the before and after variables
Compare-Object $before $after -Property Length, Name
# Create new object using New-Object
$Mytruck = New-Object Object
# Adding Properties Using Add-Member
Add-Member -MemberType NoteProperty -Name Color -Value Red -InputObject $Mytruck
# Adding using shortened parameter names
Add-Member -Me NoteProperty -in $Mytruck -Na Make -Value Ford
# Adding using positional parameters
Add-Member -InputObject $Mytruck NoteProperty Model "F-150 Raptor"
# Adding through a pipeline
$Mytruck | Add-Member NoteProperty Cab SuperCrew
# Accessing specific property
$Mytruck.Make
# Adding a new method
Add-Member -MemberType ScriptMethod -InputObject $Mytruck -Name Drive -Value { "Going on a roadtrip" }
# Adding a method by parameter position
Add-Member -InputObject $Mytruck ScriptMethod Accelerate { "Skinny pedal on the right" }
# Adding a method through a pipeline
$Mytruck | Add-Member ScriptMethod Park { "Finding a spot" }
# Accessing a method
$Mytruck.Park()
$Mytruck.Drive()
\ No newline at end of file
# Equals
2 -eq 2
2 -eq 3
1,2,3 -eq 2
"abc" -eq "abc"
"abc" -eq "abc", "def"
"abc", "def" -eq "abc"
# Not Equals
"abc" -ne "def"
"abc" -ne "abc"
"abc" -ne "abc", "def"
"abc", "def" -ne "abc"
# Greater Than
8 -gt 6
7, 8, 9 -gt 8
# Less Than
6 -lt 8
7, 8, 9 -lt 8
# Greater than or equal to
6 -ge 6
5, 6, 7 -ge 6
# Less than or equal to
6 -le 6
5, 6, 7 -le 6
# like
"Inky" -like "*"
"Blinky" -like "Bl?nky"
"Pinky" -like "[L-Q]inky"
"Clyde" -like "[ABC]lyde"
# notlike
"Inky" -notlike "*"
"Blinky" -notlike "Bli?nky"
"Pinky" -notlike "[A-F]inky"
"Clyde" -notlike "[DEF]lyde"
# match
$text = "Your account username is ACE4495"
$pattern = '([A-F]{3})(\d{4})'
$text -match $pattern
# contains
$ghosts = 'Inky', 'Blinky', 'Pinky', 'Clyde'
$ghosts -contains 'pinky'
\ No newline at end of file
# Display only the services with the status of 'running'
Get-Service | Where-Object{$_.Status -eq 'running'}
# Show txt files with sizes greater than 100
Get-ChildItem *.txt | Where-Object{$_.Length -gt 100}
# Display all processes whose company name begins with 'micro'
Get-Process | Where-Object {$_.Company -like 'micro*'} | Format-Table Name, Description, Company
$x = 5
# Example 1
# Equal To
if($x -eq 5) {
Write-Host "Condition is true - x is equal to 5" -ForegroundColor Blue
}
# Less Than
if($x -lt 6) {
Write-Host "x is less than 6" -ForegroundColor Green
}
# Greater Than
if($x -gt 4) {
Write-Host "X is greater than 4" -ForegroundColor Cyan
}
# Greater Than or Eqaul To
if($x -ge 2) {
Write-Host "x is greater than or equal to 2" -ForegroundColor Yellow
}
# Less Than or Equal To
if ($x -le 9) {
Write-Host "x is less than or equal to 9" -ForegroundColor Gray
}
# Not Equal
if($x -ne 8) {
Write-Host "x is not equal to 8" -ForegroundColor Red
}
# Example 2 Case-insensitive comparisons
if("ABC" -ieq "Abc") {
Write-Host "Example 1 - Condition satisfied" -ForegroundColor Green
}
else {
Write-Host "Example 1 - Condition NOT satisfied" -ForegroundColor Red
}
# Example 3 Case-Sensitive
if("ABC" -ceq "Abc") {
Write-Host "Example 1 - Condition satisfied" -ForegroundColor Green
}
else {
Write-Host "Example 1 - Condition NOT satisfied" -ForegroundColor Red
}
$ZeroWing = "All your base are belong to us"
$var = "base"
if ($ZeroWing -contains $var)
{
$x = "The line is " + $true
}
else
{
$x = "This is a lie and " + $false
}
$x
\ No newline at end of file
$x = 2
if ($x -eq 5) {
Write-Host "Bob"
}
elseif ($x -eq 4) {
Write-Host "Sue"
}
elseif ($x -eq 2) {
"Tom"
}
elseif ($x -gt 1) {
"Mary"
}
else {
"Who am i?"
}
$num = (Get-Random -Minimum 0 -Maximum 10)
if($num -lt 5) {
"$num is less than 5"
}
elseif($num -eq 5) {
"$num is exactly 5"
}
else {
"$num is greater than 5"
}

# Example 1 Multiple values meet condition
Switch(10)
{
(1 + 9) {
"Congrats, you did adds right"
}
(1 + 10) {
"This be wrong"
}
(11 - 1) {
"Congrats, you did minus right"
}
(1 - 11) {
"Congrats, you did math wrong"
}
}
# Example 2 multiple values
$num = (Get-Random -Minimum 0 -Maximum 10)
Switch($num) {
{$_ -le 3} { "Some number from 1 to 3" }
6 { "It is the number 6" }
{(($_ -ge 6) -and ($_ -le 9))} { "Some number from 7 to 9" }
{$_ -gt 2} { "It is greater than 2"}
{(($_ -ge 5) -and ($_ -le 8))} { "Some number from 5 to 8" }
{$_ -le 7} { "It is less than 7"}
}
# Example 3 using default
$val = "Meg"
Switch($val) {
Peter { "That's the father" }
Lois { "That's the mother" }
Stewie { "That's the youngest child" }
Chris { "That's the oldest child" }
default { "Nobody cares about you $val" }
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment