Difference between Clear-Variable and NULL

I often use variables declared in a script scope to avoid problems with functions and their scope. I declare these variables as follows:

New-Variable -Name test -Option AllScope -Value $null 

... or sometimes I switch existing variables like this to use them comprehensively:

 $script:test = $test 

When I want to clean them, I either use this:

 Clear-Variable test -Scope Script 

... or I just use this:

 $test = $null 

Is there any difference? What should I prefer and why?

+7
source share
2 answers

From get help:

The Clear-Variable cmdlet deletes the data stored in a variable, but does not delete the variable. As a result, the value of the variable is NULL (empty). If a variable has the specified data or object type, Clear-Variable saves the type of object stored in the variable.

So, Clear-Variable and $var=$null are almost equivalents (except for the record being saved). The exact equivalent would be to make $var=[mytype]$null .

You can check it yourself:

 $p = "rrrr" Test-Path variable:/p # => $true $p = $null Get-Member -InputObject $p # => error $p = [string]$null Get-Member -InputObject $p # => it is a string 

And to answer the question, what could be the following question: how to completely remove a variable (since the missing variable is different from the zero variable)? Just do

 rm variable:/p Test-Path variable:/p => $false 
+7
source

In addition to Marcanpilami, a useful answer :

Note. To remove the undefine variable as a whole, use Remove-Variable <name> [-Scope <scope>] .

If $test not defined using Set-Variable -Option AllScope ,

  • Clear-Variable test -Scope Script

as well as

  • $test = $null

usually not equivalent .

(They exist with Set-Variable -Option AllScope , but then the -Scope argument becomes inappropriate, because only one instance of the variable exists (conceptually) in all areas.)

$test = $null - if not performed to the same extent when the test variable was originally created - implicitly create the test variable in the current area (and assign $null to it), and leave the original variables untouched. For more information on variable scales in PS, see this answer .

Note that the syntax with assignment variables also offers the definition of scope using the scope prefix, but it is limited to global , script and local (default): $global:test = $null , $script:test = $null , $local:test = $null

There is also private coverage: a local option that prevents the visibility of visible objects from descendants - see this answer .

If you guaranteed that you are targeting the same area, the two forms above are functionally equivalent: they assign the target $null variable. [one]

However, using Clear-Variable you can do two things that $<scope>:testing =... not :

  • the -Scope parameter also takes a numerical value that indicates the scope relative to the current scope : 0 - current scope, 1 - parent scope, etc.

  • you can target multiple variables (either as an array of names or using wildcards)


[1] Pittum :

Please note that if the target variable is limited by type ("excellent notation" was assigned, for example, [int] $i = 1 ), the type is preserved - whether using $testing = $null or Clear-Variable - and conversion may occur implicitly type that may have unexpected results or even crash :

 [int] $i = 1 # type-constrain $i as an integer Clear-Variable i # equivalent of $i = $null $i # !! $i is now 0 (!), because [int] $null yields 0 [datetime] $d = 1 # type-constrain $d as DateTime Clear-Variable d # !! FAILS, because '$d = $null' fails, given that # !! $null cannot be converted to [datetime] 
+3
source

All Articles