To complement Marcanpilami's helpful answer:
Note: In order to remove (undefine) a variable altogether, use Remove-Variable <name> [-Scope <scope>]
.
Unless $test
is defined with Set-Variable -Option AllScope
,
Clear-Variable test -Scope Script
and
are NOT generally equivalent.
(With Set-Variable -Option AllScope
they are, but then the -Scope
argument becomes irrelevant, because then only one instance of the variable exists (conceptually), across all scopes.)
$test = $null
- unless executed in the same scope as when variable test
was originally created - will implicitly create a test
variable in the current scope (and assign $null
to it), and leave the original variable untouched. For more on variable scoping in PS, see this answer
Note that variable-assignment syntax offers scoping too, via a scope prefix, but it is limited to global
, script
, and local
(the default): $global:test = $null
, $script:test = $null
, $local:test = $null
There's also the private
scope: a variation of local
that prevents descendant scopes from seeing a variable - again, see this answer.
If you've ensured that you are targeting the same scope, the two forms above are functionally equivalent: they assign $null
to the target variable.[1]
However, using Clear-Variable
allows you to do two things that $<scope>:testing = ...
doesn't:
the -Scope
parameter also accepts a numeric value that indicates the scope relative to the current scope: 0
is the current scope, 1
is the parent scope, and so on.
you can target multiple variables (either as an array of names or using wildcards)
[1] Pitfall:
Note that if the target variable is type-constrained (was assigned with "cast notation"; e.g., [int] $i = 1
), the type is retained - whether using $testing = $null
or Clear-Variable
- and an implicit type conversion may occur, which can have unexpected results or fail altogether:
[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]