Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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} 
like image 221
Shirko Shwan Avatar asked Feb 24 '15 13:02

Shirko Shwan


People also ask

How do you assign a null value to a variable?

A variable can be explicitly assigned NULL or its value been set to null by using unset() function.

How do you use null in PowerShell?

To send data to an Out cmdlet, use a pipeline operator ( | ) to send the output of a Windows PowerShell command to the cmdlet. You can also store data in a variable and use the InputObject parameter to pass the data to the cmdlet. For more information, see the examples. Out-Null does not return any output objects.

How do you assign a value to a variable in PowerShell?

To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it. The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable .

What is a null value in PowerShell?

PowerShell $Null is an automatic variable that holds nothing but unidentified or absent value and it is also considered as an Object.


2 Answers

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 
like image 158
hdev Avatar answered Sep 23 '22 14:09

hdev


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.

like image 21
Krishna Avatar answered Sep 23 '22 14:09

Krishna