Skip to content

Instantly share code, notes, and snippets.

Last active Mar 17, 2021
What would you like to do?
Unwrapping STDERR in Powershell
STDERR strings get wrapped in an ErrorRecord. Unwrap those back into strings.
function Convert-StderrString {
# Why can this not be an advanced function with [CmdletBinding()] ?
# Somehow it doesn't work if I put that in.
begin {
process {
if ($PSItem -is [System.Management.Automation.ErrorRecord]) {
# yeah OK I haven't figured out how to say 'and' yet
if ($PSItem.FullyQualifiedErrorId -eq "NativeCommandError") {
if ($PSItem.TargetObject -is [string]) {
end {
Set-Alias -Name fix-stderr -Value Convert-StderrString
Copy link

KacperMucha commented Nov 14, 2019

$PSItem -is [System.Management.Automation.ErrorRecord] -and $PSItem.FullyQualifiedErrorId -eq "NativeCommandError" -and $PSItem.TargetObject -is [string]


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment