3 min read
Typed PowerShell Arrays and Generic Lists
Understanding the different array types in PowerShell is essential for efficient data management and automation in your...
Unlocking the Power of PowerShell: Tips for Success
As $error is a global variable, consider using your own logging variable for error handling so you don't affect other users (and vice versa).
Whenever PowerShell encounters an unhandled exception, it is stored in the global $error variable. This can be used for error handling. Here is typical code:
# clear error log
$error.Clear()
# hide error messages from the user
$ErrorActionPreference = 'SilentlyContinue'
# perform cmdlet actions, i.e. find log files
$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log
# perform more actions, i.e. access a service that does not exist
$service = Get-Service -Name notThere
# restore default error action
$ErrorActionPreference = 'Continue'
# check errors after the fact
$errorCount = $error.Count
Write-Host "There were $errorCount errors."
# output error details
$error.CategoryInfo | Select-Object -Property Category, TargetName
This type of error handling is generally perfect when you want cmdlets to first complete their work, and not aborting them.
However, $error is a global variable, so when you clear it, you may be affecting other users. Likewise, your own error logging may be affected by commands you use from 3rd party modules (as they may clear this variable, too, at any time).
That’s why it is safer to use your own error logging variable. Note how the code below is not touching $error at all:
# perform cmdlet actions, i.e. find log files
$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log -ErrorAction SilentlyContinue -ErrorVariable myErrors
# perform more actions, i.e. access a service that does not exist
$service = Get-Service -Name notThere -ErrorAction SilentlyContinue -ErrorVariable +myErrors
# check errors after the fact
$errorCount = $myErrors.Count
Write-Host "There were $errorCount errors."
# output error details
$myErrors.CategoryInfo | Select-Object -Property Category, TargetName
Instead, the ‑ErrorVariable parameter specifies the name of your own variable that you’d like to use for error logging, in this case "myErrors". This instructs the cmdlet to generate the new variable $myErrors. If you append the variable name by "+", the cmdlet uses an existing variable so you can log the errors of more than one cmdlet in this variable.
If you’d like to log all errors of all cmdlets in your script like this, you may want to change $ErrorActionPreference to "SilentlyContinue", and use $PSDefaultParameterValues to always use ‑ErrorVariable on all of your cmdlets:
# clear error variable if it exists from a previous run
$variableExists = Get-Variable -Name myErrors -Scope 0 -ErrorAction Ignore
if ($variableExists) { Remove-Variable -Name myErrors }
# add private error logging to all cmdlets
$ErrorActionPreference = 'SilentlyContinue'
$PSDefaultParameterValues.Add('*:ErrorVariable', '+myErrors')
# perform cmdlet actions, i.e. find log files
$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log
# perform more actions, i.e. access a service that does not exist
$service = Get-Service -Name notThere
# check errors after the fact
$errorCount = $myErrors.Count
Write-Host "There were $errorCount errors."
# output error details
$myErrors.CategoryInfo | Select-Object -Property Category, TargetName
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.
The poster is available for download and in paper form.
Jan 28, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Understanding the different array types in PowerShell is essential for efficient data management and automation in your...
Jan 21, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Optimizing PowerShell arrays is key to improving script performance and ensuring efficient data handling in your...
Jan 16, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Here is part two of our tips on PowerShell arrays.
Let’s explore some common caveats with arrays, starting with the...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
----------------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
----------------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.