When a command returns an error, I get like an error message, plus what looks like a full stack of the error:
C:\> dir foo
dir : Cannot find path 'C:\foo' because it does not exist.
At line:1 char:1
+ dir foo
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\foo:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Is there a way to only see the error (that's the only thing usefull for me) and not display the full stack ?
Like:
C:\> dir foo
dir : Cannot find path 'C:\foo' because it does not exist.
You need to catch the error if you want to control how or what is displayed:
try {
dir foo -ErrorAction Stop
} catch {
Write-Host $_
}
Sometimes you'll need to add -ErrorAction Stop
(or $ErrorActionPreference = 'Stop'
) to ensure that all errors are terminating (so they can be caught).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With