User-Defined Variables U
nlike some scripting languages, Power-
Shell doesn’t require
you to explicitly
declare a variable
before you use it.
You simply assign it
a value. For example,
the following
statement assigns
the string one two to
the $var1 variable:
$var1 = "one two"
When you define
a variable in this way,
you’re actually calling the New-Variable cmdlet and providing two
arguments to it: the variable’s name and
value. For example, the following statement
achieves the same results as the preceding
statement:
New-Variable var1 "one two"
In this statement, note that the variable’s
name isn’t preceded with a dollar sign.
When a variable’s name is an argument to
the New-Variable cmdlet or to the Set-Variable,
Clear-Variable, or Remove-Variable
cmdlet (all of which I’ll cover shortly), you
don’t include the dollar sign.
One advantage of using the New-Variable
cmdlet to create a variable is that
you can use other cmdlet parameters. For
example, you can use the -option parameter
to define the variable as read-only:
New-Variable var1 "one two" `
-option ReadOnly
In this case, I’ve specified ReadOnly as an
argument to the -option parameter. You can
also specify other values as arguments to
this parameter. If you specify more than one
argument, you need to use commas to separate
them. For a complete list of acceptable
arguments, see the New-Variable Help file.
When you set a variable as read-only,
you can’t change the variable’s value unless
you force the change. However, before I get
into the details of how to do that, let’s first
take a look at how to update a variable. Typically,
the simplest way to change a variable’s value is to use the = operator
to assign the new value:
$var1 = "three"
You can achieve the same
result by using the Set-
Variable cmdlet:
Set-Variable var1 "three"
Both these statements will reset the value,
but only if the variable is
not defined as
read-only. If it is read-only, you’ll receive
an error and you won’t be able to update
the value. The way to work around this
issue is to use the -force parameter:
Set-Variable var1 “three” -force
Now the variable will be assigned the new
value. As you saw with the New-Variable
cmdlet, the advantage to using the Set-Variable
cmdlet is that you can use additional
parameters.
Now let’s take a look at how to clear a
variable’s value. One approach you can
take is to set the value to null by using the
$null built-in variable:
$var1 = $null
Or, you can use the Clear-
Variable cmdlet to achieve
the same result:
Clear-Variable var1
However, as before, if the
variable is defined as readonly,
both these commands
will fail. Once again, the
workaround is to add the
-force parameter:
Clear-Variable var1 `
-force
Finally, if you want to delete
a variable altogether, you
can use the Remove-Variable
cmdlet:
Remove-Variable var1
Not surprisingly, this command will return an error if the variable is
read-only. However, the Remove-Variable
cmdlet also supports the -force parameter:
Remove-Variable var1 -force
In most cases, you’ll probably find that
you can simply use the = operator to create,
change, and clear variables. However, if you
want to use the New-Variable, Set-Variable,
Clear-Variable, and Remove-Variable cmdlets,
you can find more information about
them in their respective Help files.
Continue on Page 3