Datasheet

10 CHAPTER 1
USING POWERSHELL WITH ACTIVE DIRECTORY
Profile Scripts
e PowerShell pro le is really a script that runs automatically when you open
PowerShell. PowerShell scripts are covered in more detail later in this chapter, but for
now just know that you can execute various PowerShell commands, save variables, and
de ne functions in your PowerShell pro le. For example, suppose you frequently put
your computer into Hibernate mode. You can create a custom function (functions are
also covered later in this chapter) that puts the computer in Hibernate mode by typing
in a simple command. To do this, add the following line to your PowerShell pro le:
function hib { shutdown -h -t 0 }
is function tells PowerShell to run the
shutdown
command with the
-h
(hiber-
nate) switch whenever you type in
hib
. Because your PowerShell pro le is loaded
every time you open PowerShell, the
hib
command will always be available to you.
Creating a Profile
By default, you have a PowerShell pro le de ned, but the  le doesn’t exist and there-
fore isn’t executed. You determine what your current pro le script is by displaying
the contents to the
profile
variable. To see the
profile
variable, type the follow-
ing command:
$profile
You can determine whether the pro le script exists by the running the
Test-Path
cmdlet, which returns true if the  le exists and false if it doesn’t:
Test-Path $profile
If the pro le script doesn’t exist, you can create it using any text editor, such as
Notepad
.exe
. You can also run the
New-Item
command to create the pro le script  le:
PS C:\Users\Administrator> New-Item -Path $profile -ItemType File -Force
Directory: C:\Users\Administrator\Documents\WindowsPowerShell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/29/2010 3:29 PM 0 Microsoft.PowerShell_profile.ps1
A er the pro le script is created, you can use Notepad to edit it:
PS C:\Users\Administrator> notepad.exe $profile
If you try to create a pro le script now, you may receive the error shown next when
PowerShell attempts to load the pro le script.  is is a security measure put in place
c01.indd 10c01.indd 10 5/12/2011 1:07:44 PM5/12/2011 1:07:44 PM