Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to script a dcdiag test to alert me if it finds any errors. I thought I may able to do this in powershell by...

$test = dcdiag 2>$err

I don't have any errors from dcdiag at the moment so I couldn't test that directly, but I wrote another ps script to throw an exception hoping I could test this method using that script. This didn't work using the method above so I opted for

try{$test = dcdiag}catch{$err = $_.Exception.Message}

It worked for my test case, but I don't know if this will pick up stderr from dcdiag.

I would like to know how I should best achieve stderr redirect to a variable in powershell given I would like to use it with dcdiag?

share|improve this question
up vote 11 down vote accepted

try..catch wouldn't help in this case.

You might want to do:

$test = dcdiag 2>&1
$err = $test | ?{$_.gettype().Name -eq "ErrorRecord"}
#error has occurred
share|improve this answer
Ah, I didn't say in the original question; is it possible to keep both stdout and stderr? i.e $test = stdout and $err = stderr? – Ablue Dec 14 '11 at 2:14
@Ablue - $test will have both, that is why I am filtering out the error to $err. – manojlds Dec 14 '11 at 2:15
Thanks a million :) – Ablue Dec 14 '11 at 2:17
Make sure $ErrorActionPreference is not set to "SilentlyContinue". When it is the errors stream is not available. – cmcginty Oct 28 '15 at 21:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.