We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Suppressing warnings in a PowerShell script

Julian123
Julian123 asked
on
Medium Priority
15,318 Views
Last Modified: 2012-05-11
I am writing a script that moves Exchange 2010 mailboxes using the new-moverequest cmdlet. Whenever one uses the -baditemlimit parameter, the cmdlet displays a warning in yellow text. I want to suppress the warning. In the code snippet below, I have tried setting the warningpreference value as well as using the -warningaction parameter for the cmdlet itself as you can see. In addition, I have also tried setting the warningpreference value by just typing it into the powershell window prior to running the .ps1 file containing the code below.

Even with all three of those things put together, I still get the yellow warning text for each invocation of the commandment, meaning it appears dozens of times. How do I get rid of it? If someone could please show me how the code below needs to be edited, I would appreciate it.

Thanks.

$WarningPreference = "SilentlyContinue"
$userList = Import-Csv $csvfile 

foreach ($user in $userList)
{
    write-host "Creating move request for" $user.identity "to database" $targetDB
    new-moverequest -identity $user.Identity  -targetdatabase $targetdb -baditemlimit 100 -acceptlargedataloss -warningaction "SilentlyContinue"
}

Open in new window

Comment
Watch Question

Robert SchuttSoftware Engineer
CERTIFIED EXPERT

Commented:
Are you using PowerShell 1.0? Apparently the warningAction option only comes in v2. But I can't try it out here.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
How about piping stderr to null? That is:
$userList = Import-Csv $csvfile 
foreach ($user in $userList)
{
    write-host "Creating move request for" $user.identity "to database" $targetDB
    new-moverequest -identity $user.Identity  -targetdatabase $targetdb -baditemlimit 100 -acceptlargedataloss 2> nul
}

Open in new window

Author

Commented:
It is powershrll v2 on w2k8 r2.

I can't pipe all error output as I need to see errors, just suppress the warnings.
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
You can use $WarningPreference = "SilentlyContinue".
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.