PnP
PnP

Reputation: 3185

Turn off echo out Powershell

I have a command which gets share sizes - however it spams the console with errors due to permissions based on who runs the script naturally.

 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force | Measure-Object -Property Length -Sum ).Sum/1GB)

I would like to suppress the errors, like turning ECHO OFF if possible?

Upvotes: 1

Views: 17124

Answers (2)

jbsmith
jbsmith

Reputation: 1666

You can add the ErrorAction parameter to your call to Get-ChildItem (I assume this is where the errors are coming from), like so:

 $shareSize = [math]::round((Get-ChildItem $($share.path) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum ).Sum/1GB)

Please look into ErrorAction and the $ErrorActionPreference built-in variable (get-help about_Preference_Variables) for more detail. And be careful with these options -- it's usually not a good idea to hide errors.

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

You can suppress error messages by redirecting the error stream to $null, e.g.:

[math]::round((Get-ChildItem $($share.path) -Recurse -Force 2>$null

Upvotes: 5

Related Questions