Initialize (null) variables in PowerShell scripts


Use the Remove-Variable cmdlet to initialize (null) variables in PowerShell scripts.

The alias is rv. Specify the name of the variable as an argument, but omit the $ or an error will result.

PS C:\work_ps> $a=1;$a;$a;
PS C:\work_ps> $a=1;$a;rv a;$a;
PS C:\work_ps>

The first command is executed twice with $a;$a;, so “1” is displayed twice.

The next command initializes by typing rv a, so the second $a; is null, so only one “1” is displayed.

There is an easier way to check if it is null.

[string]::isnullorempty(variable name);

Type as above; if True is displayed, it is Null; if False is returned, it is not Null.

PS C:\work_ps> $a=1;rv a;[string]::isnullorempty($a);
PS C:\work_ps>


Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL