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
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