Converting a Windows Error ID into Friendly Text

by Mar 8, 2018

When you call low level functions from PowerShell, often you get back a numeric return value. If the return value came from a Windows API function, here is a super easy way to translate it into a meaningful text:

For example, whenever an API function fails due to missing access, it returns the value 5. Let’s translate it to a meaningful text:

PS> New-Object -TypeName ComponentModel.Win32Exception(5)
Access is denied  

In PowerShell 5, you can also use this syntax:

PS> [ComponentModel.Win32Exception]::new(5)
Access is denied

Check this out:

1..200 | ForEach-Object { '{0} = {1}' -f $_, (New-Object -TypeName ComponentModel.Win32Exception($_)) }

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at

Twitter This Tip! ReTweet this Tip!