Powershell query

Soldato
Joined
1 Feb 2006
Posts
8,188
Somebody please tell me what the purpose of this > $null is? If the value is a null will it stop a script running? Is this another way of saying 'only continue if the result of the expression is not null'?

Code:
Get-SomeCmdlet [b]> $null[/b]
 
It means to direct the console (stdout) output of the command to 'null', a black hole, in other words running the command 'silently' whether succeed or fail.
 
Ah great thanks.

Is that similar to doing -erroraction silentlycontinue and should I use this after every single command if I don't want error messages to be written to the console?
 
It's similar to > nul: in Windows and > /dev/null in unix.. try it out :p

dir

dir > nul:

It only affects the output of the command you're redirecting, the > character is the thing that says 'redirect' so any commands without this will just behave as they do by default.

It won't stop an app from wanting to ask for input if it needs it, so it's not a 'silent install' mode in that it's hands-free, it just supresses outputs from displaying.
 
Ok makes sense. I am trying to build up some automated build scripts for SharePoint so I guess I am maybe better to keep the output visible to know whether the cmdlets are doing what they are supposed to.
 
Alternatively you could direct the outputs to a log file instead.

Don't know the magic that powershell uses but to extend the previous Windows examples, 1> is for stdout (regular console output) and 2> is for stderr (sometimes reserved separately for when errors occur). With > meaning create/write the output file if it doesn't exist, and >> being create/append the output.

So you could do things like:

dir c:\ 1>> stdout.txt 2>> stderr.txt
dir p:\ 1>> stdout.txt 2>> stderr.txt

Each time you run it, the output will go to the respective file. In my case, since I don't have a p: drive the error output goes into stderr.txt to say the error message that dir generates, whereas correct listing outputs go to stdout.txt
 
PowerShell has it's own variants for exporting to a file or null. > and >> are both command based output arguments.

Some-Cmdlet | Out-Null
Some-Cmdlet | Out-File
Some-Cmdlet | Out-File -Append

-ErrorAction "SilentlyContinue" is good if you don't care if an error occurs and want to continue processing anyway.
 
Back
Top Bottom