3 min read
Decoding URLs: PowerShell's hidden gems
In this article, we explore how PowerShell handles URL encoding and decoding using two powerful APIs:...
Unlocking the Power of PowerShell: Tips for Success
Tired of juggling PowerShell error handling? Discover how to simplify your workflow with a versatile function that converts error records into easy-to-manage data objects, making troubleshooting smoother than ever:
In our mini-series, we got in touch with exceptions in various ways: through $_ in a catch block or a trap, via $error or separate error variables defined using -ErrorVariable, or by redirecting the error stream to the pipeline.
In all of these cases, you receive error objects. Here is a generic function you can use to easily convert all of these error records to convenient data objects:
filter Convert-ErrorRecord
{
[PSCustomObject]@{
Exception = $_.Exception.Message
Reason = $_.CategoryInfo.Reason
Target = $_.CategoryInfo.TargetName
Script = $_.InvocationInfo.ScriptName
Line = $_.InvocationInfo.ScriptLineNumber
Column = $_.InvocationInfo.OffsetInLine
}
}
This function works in all the error handlers we have discussed in this series.
To recapitulate, here are all four error handling strategies we discussed in this series, all using Convert-ErrorRecord to turn the exceptions into simple-to-handle data objects. Just make sure you define above Convert-ErrorRecord function by running it, before trying one of below examples:
try
{
Write-Host 'Previous Command'
$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log -ErrorAction Stop
Write-Host 'Next Command'
}
catch
{
$_ | Convert-ErrorRecord
}
Write-Host 'Previous Command'
$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log -ErrorAction Stop
Write-Host 'Next Command'
trap {
$_ | Convert-ErrorRecord
continue
}
$error.Clear()
# hide error messages to 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 | Convert-ErrorRecord
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
{
$_
}
}
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.
Oct 1, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
In this article, we explore how PowerShell handles URL encoding and decoding using two powerful APIs:...
Sep 30, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Tired of cryptic number codes cluttering your PowerShell scripts? Discover how PowerShell enums can transform your code...
Mar 11, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Most PowerShell cmdlets are simply wrappers around underlying .NET libraries. In this series, we’ll take a closer look...
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.