Datasheet
30 CHAPTER 1
•
USING POWERSHELL WITH ACTIVE DIRECTORY
Accept Script Parameters
When you write a PowerShell script, there are times when you may want the user to
feed some information into the script. ere are two ways to handle this. e rst
way is to allow the user to edit the script and add their information directly. If your
script was signed, however, this will break the signature. e second way is for your
script to accept command parameters.
Command parameters can be passed into the script when the user runs the script’s
command in PowerShell. For example, the following command uses a parameter to
tell the
Get-Process
cmdlet which computer to execute on:
Get-Process -ComputerName BAL-DC01
You’ll be working with two types of parameters in your PowerShell scripts:
positional parameters and named parameters. Positional parameters are used
based on where they show up in the command. For example, you could have a
positional parameter speci ed that uses the rst parameter in the command as the
-FirstName
parameter. e user can choose to run the script in one of the follow-
ing ways. Both are functionally equivalent:
MyScript.ps1 -FirstName Brenna
MyScript.ps1 Brenna
Use Named Parameters
Named parameters, on the other hand, require that the user indicates the param-
eter name before specifying the value. If the
FirstName
parameter in the previous
example was a named parameter, the following command would be invalid:
MyScript.ps1 Brenna
When con guring your scripts to accept parameters, you must include some code at
the beginning of your script consisting of the keyword
param
, the type of parame-
ter (for example, string), and the variable to which the parameter will be passed. By
default, the name of the variable becomes the name of the parameter, but without
the dollar sign that pre xes the variable name. Consider the following parameter
declaration in a script:
param([string]$FirstName)
c01.indd 30c01.indd 30 5/12/2011 1:07:51 PM5/12/2011 1:07:51 PM