Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make powershell be less verbose on errors?

Tags:

powershell

For instance, consider the following console transcript:

PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

PS C:\dev\windows>

Now compare it with the ordinary shell (cmd.exe):

C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.

C:\dev\windows>

The difference is obvious and I like the laconicity of cmd.exe much much more than the verbosity of the powershell.

Can I have the same laconicity in powershell? If not for all the commands then maybe just for the Remove-Item, which I use often?

like image 977
mark Avatar asked Oct 14 '11 17:10

mark


People also ask

How do I suppress errors in PowerShell script?

If you need to suppress an error, you can use the ErrorAction switch to suppress an error for a single cmdlet or an ErrorAction preference variable to suppress errors globally.

How do I ignore exceptions in PowerShell?

If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.

What is $? PowerShell?

$? Contains the execution status of the last command. It contains True if the last command succeeded and False if it failed. For cmdlets and advanced functions that are run at multiple stages in a pipeline, for example in both process and end blocks, calling this.

How do I get an error message in PowerShell?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.


1 Answers

The closest result you can get is by changing the global $ErrorView value to "CategoryView". Another way is to create your own view.

like image 93
Shay Levy Avatar answered Sep 19 '22 10:09

Shay Levy