PowerShell Error Handling - One error at the time please

Code fails - whether because of reasons out of our control, or because we didn’t consider the situation it might fail. Expired credentials, timeout connections, lack of permissions go genuine software bug - each of these are potential problem which might fall our code over. And if we don’t handle the error, our script will simply throw and terminate. It’s certainly not desired state. In this video I’ll show you different ways PowerShell can error, how to debug such errors and how to handle them - so that we are in control....

21 January 2023 · 1 min · 93 words · Kamil

PowerShell function - converting script into function with parameters

So you’ve been writing your scripts for some time and wondering how to make them more PowerShell-like, so that they can be invoked from the console like all the other cmdlets? In this quick video I’ll show you how to convert a sample script into function, add parameters and indicated to your user how to use your function with mandatory parameters and types. # Sample script which asks user for basic information, then manipulates it and # display some information # There's no way (at least easy) to pass these parameters from prompt, nor validate $Name = Read-Host -Prompt "What's your name?...

21 January 2022 · 2 min · 371 words · Kamil

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