PowerShell Hash Table - Storing key value pairs

Quick crash course into using hash tables in PowerShell. <#PSScriptInfo .VERSION 1.0.0 .GUID 50fa84c4-080d-45cd-82a8-9e8dba10b187 .AUTHOR Kamil Procyszyn .COPYRIGHT Kamil Procyszyn .PROJECTURI https://github.com/kprocyszyn/About-PowerShell .RELEASENOTES 2021 September .DESCRIPTION Link to the video: https://youtu.be/oti2l8EmAT8 Documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.1#:~:text=In%20PowerShell%2C%20each%20hash%20table%20is%20a%20Hashtable,to%20create%20an%20ordered%20dictionary%20%28System.Collections.Specialized.OrderedDictionary%29%20in%20PowerShell. #> <# About hashtable: A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs. For example, a hash table might contain a series of IP addresses and computer names, where the IP addresses are the keys and the computer names are the values, or vice versa....

14 September 2021 · 4 min · 640 words · Kamil

PowerShell Array and ArrayList - storing multiple items as a one variable

Array is a basic data structure that allows to store multiple items in one variable. In this short video, I’ll show you how to create a basic array, populate it with items, how to add, access and modify items within array. In addition, I’ll present to you how to use ArrayList - which is much more flexible array, especially for adding more items. # Link to the video: https://youtu.be/rvGd8kxXlVc # What is an array and why do I need it?...

23 August 2021 · 3 min · 546 words · Kamil

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 For loop

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

14 July 2021 · 3 min · 544 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