Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created April 13, 2024 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HauptJ/55ed6414c21904737f26c9007a4b8257 to your computer and use it in GitHub Desktop.
Save HauptJ/55ed6414c21904737f26c9007a4b8257 to your computer and use it in GitHub Desktop.
PowerShell Hash Table Example with CLI Parameters and Try Catch Error Handling
# Input CLI params with default values
param (
$number=1,
$shape="Square",
$color="Blue"
)
try {
# Ordered hash table
[hashtable]$hash = [ordered]@{Number = $number; Shape = $shape; Color = $color}
# print out hash table using Out-String
$hash | Out-String | Write-Host
# print out hash table using for loop
foreach ($h in $hash.GetEnumerator() ) {
Write-Host "$($h.Name) : $($h.Value)"
}
} catch {
Write-Host "An error occured"
Exit 1
} finally {
Write-Host "This script executed successfully"
Exit 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment