How do I assign a null value to a variable in PowerShell?

152,338

Solution 1

These are automatic variables, like $null, $true, $false etc.

about_Automatic_Variables, see https://technet.microsoft.com/en-us/library/hh847768.aspx?f=255&MSPPError=-2147217396

$NULL
$null is an automatic variable that contains a NULL or empty value. You can use this variable to represent an absent or undefined value in commands and scripts.

Windows PowerShell treats $null as an object with a value, that is, as an explicit placeholder, so you can use $null to represent an empty value in a series of values.

For example, when $null is included in a collection, it is counted as one of the objects.

C:\PS> $a = ".dir", $null, ".pdf"
C:\PS> $a.count
3

If you pipe the $null variable to the ForEach-Object cmdlet, it generates a value for $null, just as it does for the other objects.

PS C:\ps-test> ".dir", $null, ".pdf" | Foreach {"Hello"}
Hello
Hello
Hello

As a result, you cannot use $null to mean "no parameter value." A parameter value of $null overrides the default parameter value.

However, because Windows PowerShell treats the $null variable as a placeholder, you can use it scripts like the following one, which would not work if $null were ignored.

$calendar = @($null, $null, “Meeting”, $null, $null, “Team Lunch”, $null)
$days = Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
$currentDay = 0

foreach($day in $calendar)
{
    if($day –ne $null)
    {
        "Appointment on $($days[$currentDay]): $day"
    }

    $currentDay++
}

output:

Appointment on Tuesday: Meeting
Appointment on Friday: Team lunch

Solution 2

Use $dec = $null

From the documentation:

$null is an automatic variable that contains a NULL or empty value. You can use this variable to represent an absent or undefined value in commands and scripts.

PowerShell treats $null as an object with a value, that is, as an explicit placeholder, so you can use $null to represent an empty value in a series of values.

Solution 3

If the goal simply is to list all computer objects with an empty description attribute try this

import-module activedirectory  
$domain = "domain.example.com" 
Get-ADComputer -Filter '*' -Properties Description | where { $_.Description -eq $null }

Solution 4

As others have said, use $null.

However, the handling of $null is not so simple.

In lists (or, more precisely, System.Array objects) $null is treated as a placeholding object when indexing the list, so ($null, $null).count outputs 2.
But otherwise $null is treated as a flag signifying that there is no content (no object; or, more precisely, a "null-valued expression", as reported by .GetType()), so ($null).count outputs 0.
Thus

$null.count;  # Output = 0
($null).count;  # Output = 0
(, $null).count;  # Output = 1
($null, $null).count;  # Output = 2
($null, $null, $null).count;  # Output = 3

Note: the same output is returned from .count and .length in the above context.

Similarly if explicitly assigning any of the above to a variable, as in

$aaa = $null;  $aaa.count
$bbb = ($null, $null);  $bbb.count

which output, respectively, 0 and 2.

Similarly if looping with ForEach, as in

$aaa = $null;  ForEach ($a in $aaa) {write-host "Foo" -NoNewLine}
$bbb = ($null, $null);  ForEach ($b in $bbb) {write-host "Bar" -NoNewLine}

which output, respectively, nothing and BarBar.

However, note well that when operating on an individual item that has been returned from a list $null is again treated as a "null-valued expression", as can be confirmed by running

$xxx = ($null, "foo", $null);  ForEach ($x in $xxx) {write-host "C=" $x.count "| " -NoNewLine}

which outputs C= 0 | C= 1 | C= 0 | .

Share:
152,338

Related videos on Youtube

Shirko Shwan
Author by

Shirko Shwan

Updated on July 09, 2022

Comments

  • Shirko Shwan
    Shirko Shwan almost 2 years

    I want to assign a null value to a variable called $dec, but it gives me errors. Here is my code:

    import-module activedirectory
    $domain = "domain.example.com"
    $dec = null
    Get-ADComputer -Filter {Description -eq $dec}
    
    • Etan Reisner
      Etan Reisner about 9 years
      The value is $null. null is, as you found, an error.
    • arco444
      arco444 about 9 years
      That's because it should be $null. I can't believe the most basic of searches wouldn't have thrown that at you
    • Shirko Shwan
      Shirko Shwan about 9 years
      I tried that also @arco444 with no success.
    • Kev
      Kev about 9 years
      What are the errors, if using $null?
    • Deep
      Deep over 8 years
      @arco444 my search brought me here.