$null usage and output

Hi all,

On looking through a script example for confguring DSC pull server, I found out the following command:

  
$null = New-Item -ItemType container -Path $path01
$binFolderPath_IIS = Join-Path $path01 "folder"
$null = New-Item -path $binFolderPath_IIS -itemType "directory" -Force
Copy-Item $dependentBinaries $binFolderPath_IIS -Force
$muiPath_01 = Join-Path $binFolderPath_IIS $language
if (!(Test-Path $muiPath_01))
{
$null = New-Item -ItemType container $muiPath_01
}
Copy-Item $dependentMUIFiles $muiPath_01 -Force
Copy-Item $cfgfile (Join-Path $path01 "web.config") -Force
Copy-Item $svc $path01 -Force
Copy-Item $mof $path01 -Force
Copy-Item $dispatch $path01 -Force
Copy-Item $asax $path01 -Force

I state in advance that I am a newbie in PS, but so far I have seen the $null usage for

clearing the value of a variable only, see below:

PS C:\Users\user1\Desktop> $myVar = "Hello World!"

PS C:\Users\user1\Desktop> $myVar = $null

PS C:\Users\user1\Desktop> $myVar

PS C:\Users\user1\Desktop>

I guess that I am still missing something on the usage of $null in the 1st example; so

what's the purpose of $null when it sets on left of the " = "(equal sign), and the related

outcome?

Thanks in advance for your help.

Leo

I

July 17th, 2015 11:54am

Hi,

That's used to suppress console output. You can do that multiple ways, such a piping through Out-Null.

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 11:59am

At a CMD prompt (used to be called DOS??)

dir >nul:

At a PowerShell prompt:

dir >$null
dir |out-Null
$nul=dir

July 17th, 2015 12:12pm

Hi Tim,

and thanks for your prompt reply; so if I well understood, in this case the $null automatic variable can be used to speed up the script process, as no outcome's worked out and displayed.

Thanks again and have a nice week-end

Leonardo

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 5:10pm

All processing is still don.  The output to the console is thrown away.  We call the "null" device the "bit bucket'.

July 17th, 2015 5:12pm

Simple example:

$x=0
1..100 | %{$x+=$_}
$x

Now do this:

$x=0
1..100 | %{$x+=$_} > $null
$x

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 5:15pm

That pattern is used to avoid producing output.
It is used in a function or script to control what values the function or script returns.
Any error messages will still be produced.  Also any debug, verbose or warning messages will still be produced.
An example:

<##> 1/1   # output is not suppressed
1
<##> $null = 1/1   # output is suppressed

<##> #################################################

<##> 1/0
Attempted to divide by zero.
<##> $null = 1/0    # error message still produced in spite of $null =
Attempted to divide by zero.



July 17th, 2015 7:53pm

And also study this example

<##> $(1/1),2 | %{$_*2}
2
4
<##> $($null = 1/1),2 | %{$_*2}
4
<##>

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 10:32pm

Can't fool us P.T.

PS >1,2 | %{$_*2}
2
4

PS >$null,2 | %{$_*2}
2

We have all already seen the egress.

PS >$null * 666

July 17th, 2015 10:47pm

Simple example:

$x=0
1..100 | % {$x+=$_}
$x


The output is: 5050


Now do this:

$x=0
1..100 | %{$x+=$_} > $null
$x



The output is: 5050 (again)

So, what

\_()_/
is happening?

I'm sure I know: read about_operators and about ForEach-Object cmdlet (the > $null does nothing in your code above).

Free Windows Admin Tool Kit Click here and download it now
July 18th, 2015 12:20am


Any error messages will still be produced.  Also any debug, verbose or warning messages will still be produced.

Yes, those outputs are produced. Why shouldn't they be produced? After all, they are all independent.


July 18th, 2015 12:24am

also

<##> ,2 | %{$_*2}
4
<##>

Free Windows Admin Tool Kit Click here and download it now
July 18th, 2015 12:47pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics