About PowerShell is educational series of videos that teach you about different aspects of PowerShell usage.
All the code is available on GitHub.
About PowerShell is educational series of videos that teach you about different aspects of PowerShell usage.
All the code is available on GitHub.
One switch statement can replace multiple if/else statements, it makes code more readable and is really easy to use - there’s no reason why you shouldn’t give it a try! # Link to the video: https://youtu.be/EqJ0lBO1rM4 # Documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.1 # Basic version <# switch ( test-value ) { condition {do-stuff} another condition {do-stuff} } #> # This will print out Green switch (2) { 0 { "Blue" } 1 { "Yellow"} 2 { "Green"} 3 { "Red"} } # Work with variable $number = 1 switch ($number) { 0 { "Blue" } 1 { "Yellow"} 2 { "Green"} 3 { "Red"} } # Assign variables within scriptblock $number = 3 switch ($number) { 0 { $result = "Blue" } 1 { $result = "Yellow"} 2 { $result = "Green"} 3 { $result = "Red"} } Write-Host "The result is: $result" -ForegroundColor $result # We can also assign statement to variable $number = 0 $result = switch ($number) { 0 { "Blue" } 1 { "Yellow"} 2 { "Green"} 3 { "Red"} } Write-Host "The result is: $result" -ForegroundColor $result # Use default in case there's no match $number = 8 $result = switch ($number) { 0 { "Blue" } 1 { "Yellow"} 2 { "Green"} 3 { "Red"} default { Write-Warning "Unknown value, defaulting to White" "White" } } Write-Host "The result is: $result" -ForegroundColor $result # Strings can also be matched # This also shows working on expression switch ( (Get-Host)....
For loop - do you actually need it, since there’s foreach loop? Turns out, yes - there are situations where for loop comes very handy. In this video I’ll show you the basic syntax of the for loop, going through nested for loops to end up with real case scenario from Azure Application Insights. # Link to the video: https://youtu.be/YQnBVn-9SN0 # Documentation: # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_for?view=powershell-7.1 # Basic syntax: # for (Initial Value/statement; Condition; Repeat) { Run my code } # Initial value: Set this is up before starting the loop # Condition: For loops ends, when condition evaluates to false; or it keep running as lon as condition is true # Repeat: Do this after every loop for ($MyVariable = 0; $MyVariable -lt 10; $MyVariable = $MyVariable + 1) { '$MyVariable is {0}' -f $MyVariable Start-Sleep -Seconds 2 } for ($MyVariable = 0; $MyVariable -lt 99; $MyVariable++) { '$MyVariable is {0}' -f $MyVariable } # We can also decrease it for ($MyVariable = 10; $MyVariable -gt 5; $MyVariable = $MyVariable - 1) { '$MyVariable is {0}' -f $MyVariable } # or specify variable outside if $outside = 7 for (; $outside -lt 15; $outside++) { '$Outside is {0}' -f $outside } #Looping through array $pets = @("Cat", "Dog", "Fish", "Turtle") $pets....
In this video we are exploring how to use If statement in various scenarios. If statement allows you to take tide control over the execution of your code, by dictating your script what to do in a given situation. # Link to the video: https://youtu.be/j8Ubwv8ApdU # Documentation: # Operators: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.1 # About If: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_if?view=powershell-7.1 # Basic version # if something is true then do this if ($true) { "This is true" } if ($false) { "This is false" } # if something is true then do this, otherwise do that if ($true) { "This is true" } else { "This is false" } if ($false) { "This is true" } else { "This is false" } # let's do some actual example if ( 5 -gt 3 ) { "This is more!...