PowerShell Switch Statement - Different take on branching

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)....

4 August 2021 · 3 min · 457 words · Kamil

PowerShell If statement - controlling the flow of your code

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!...

3 July 2021 · 3 min · 608 words · Kamil

PowerShell for IT Professionals [#15] – Scripting part 2 – IF and psobject

In this lesson we carry on writing the scripting by gathering requirements and putting them together as comments in code. Then we will retrieve OS information with the help of WMI and display it on the screen with Write-Host. Although using Write-Host is easy to use, it doesn’t really allow us to do very much e.g. we cannot export information to the CSV, therefore we change it and start using custom PSObject – that way our script will start returning information like a regular PowerShell command....

5 October 2020 · 1 min · 206 words · Kamil