Datasheet

24 CHAPTER 1
USING POWERSHELL WITH ACTIVE DIRECTORY
NOTE
Some other development languages require you to defi ne what kind of data
your variables will hold up front, but this isn’t the case with PowerShell.
Variables in PowerShell all begin with a dollar sign (
$
). For example, if you
wanted to create a variable and hold a sentence in it, you would use the following
PowerShell command:
$MySentence = “Active Directory Rules!”
is example stores the text string “Active Directory Rules!” into the variable called
$MySentence
. You can name variables anything you want, as long as they begin
with a dollar sign. Here’s another example, except this time we’ll store a number:
$MyInteger = 1234567890
Notice that we didn’t use the quotation marks this time. You only use quotation
marks for text strings—you have to use quotation marks for text so PowerShell
knows you’re using text and not trying to run a PowerShell cmdlet instead.
You can store data other than text and numbers in variables. For example, the fol-
lowing command is valid in PowerShell:
$DirectoryListing = dir
is command runs the PowerShell command
dir
, which lists the  les and
folders in the current directory and stores the output in the variable called
$DirectoryListing
.
$DirectoryListing
doesn’t contain the text of the
output from the
dir
command. Instead,
$DirectoryListing
stores each of the
les and folders as objects with their own properties.  is allows you to do some
interesting things. For example, you could go through the  le and folder objects in
the
$DirectoryListing
variable and rename them.
Variables can contain many types of objects.  e type of object that is stored in the
variable depends on the output of the command that is populating the variable.  e
dir
command we used in the example works with  les and folders, so it stored  le
and folder objects. If you used another command, such as
Get-Service
, it would
store the objects that represent the computer’s services and their associated proper-
ties into the variable.
You can see what variables are currently being used by running the
Get-Variable
cmdlet as shown in Figure 1.10.
c01.indd 24c01.indd 24 5/12/2011 1:07:49 PM5/12/2011 1:07:49 PM