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
Here comes the next strategy to handle errors, after learning about realtime and delayed error handling before.
There are two default error handling strategies covered earlier in this series:
Here is the third approach that can combine the benefits of the both default approaches: It handles errors in real time immediately when they occur, and it does not require cmdlets to abort or use ‑ErrorAction Stop:
Get-ChildItem -Path C:\Windows -Depth 1 -Filter *.log 2>&1 |
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
$text = $_ | Convert-ErrorRecord | Out-String
Write-Host $text
}
else {
$_
}
}
As you can see, the cmdlet (in this example Get‑ChildItem) does not use ‑ErrorAction at all. It runs completely unaffected from any error handling.
Instead, all emitted errors from stream #2 are redirected to the regular output stream #1 and can now travel down the pipeline. This way, they can stream in real time.
The ForEach‑Object cmdlet can then separate the exceptions from stream #2 from the regular output from stream #1, and handle the received exceptions in real time, while at the same time let the cmdlet do its work uninterruptedly.
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.