Windows
PowerShell
3.0
Step by Step
Contents
Foreword xix
Introduction xxi
Chapter
1Overview
ofWindows PowerShell 3.0
1Understanding
Windows PowerShell 1Using
cmdlets 3Installing
Windows PowerShell 3Deploying
Windows PowerShelltodown-leveloperating
systems 4Using
command-line utilities 5Security
issues with WindowsPowerShell 6Controlling
execution of PowerShell cmdlets 7Confirming
actions 8Suspending
confirmation of cmdlets 9Working
with Windows PowerShell 10Accessing
Windows PowerShell 10Configuring
the Windows PowerShell console 11Supplying options
forcmdlets 12Working
with thehelp options
13Exploring
commands:step-by-step
exercises 19Chapter
1quick
reference 22What do you think of thisbook?We want to hear fromyou!
Microsoft is interested inhearingyourfeedbackso we cancontinuallyimproveour books andlearningresourcesforyou. Toparticipateinabrief online survey,pleasevisit:
Chapter
2Using
Windows PowerShell Cmdlets23
Understanding
the basics of cmdlets 23Using
the Get-Childltem cmdlet 24Obtaining
adirectory listing
24Formatting
adirectory listing using
the Format-List cmdlet 26Using
theFormat-Wide cmdlet 27Formatting
adirectory listing using
Format-Table 29Formatting
outputwith Out-GridView 31Leveraging
the powerof Get-Command 36Searching
for cmdletsusing
wildcard characters 36Using the Get-Member cmdlet 44
Using
the Get-Member cmdletto examineproperties
and methods 44
Using
theNew-Object
cmdlet 50Creating
andUsing
the wshShellObject
50Using
theShow-Commandcmdlet 52 Windows PowerShell cmdletnaming helps
you learn 54Windows PowerShell verb grouping 54
Windows PowerShell verb distribution 55
Creating
aWindows PowerShellprofile
57Finding
all aliases foraparticular object
59Working
with cmdlets:step-by-step
exercises 59Chapter
2quick
reference 63Chapter
3Understanding
andUsing
PowerShell Providers
65
Understanding
PowerShellproviders
65Understanding
the aliasprovider
66Understanding
the certificateprovider
68Understanding
the environmentprovider
76Understanding
thefilesystem provider
80Understanding
the functionprovider
85Using
theregistry provider
to manage the Windowsregistry
87Thetwo
registry
drives 87Understanding
the variableprovider
97Exploring
PowerShellproviders: step-by-step
exercises 101Chapter
3quick
reference 106Chapter
4
Using
PowerShell
Remoting
and Jobs 107Understanding
Windows PowerShellremoting
107Classic remoting 107
WinRM 112
Using
Windows PowerShelljobs
119Using
Windows PowerShellremoting: step-by-step
exercises 127Chapter
4quick
reference 130Chapter
5Using
PowerShell
Scripts
131
Why
write WindowsPowerShell scripts? 131Scripting
fundamentals 133Running
Windows PowerShellscripts
133Enabling
Windows PowerShellscripting support
134Transitioning
from command linetoscript
136Running
Windows PowerShellscripts
138Understanding
variables and constants 141Useofconstants 146
Using
the Whilestatement 147Constructing
the Whilestatement in PowerShell 148 Apractical example
ofusing
the While statement 150Using
special
features of Windows PowerShell 150Using
the Do...Whilestatement 151Using
the rangeoperator
152Operating
over anarray 152Using
the Do...Untilstatement 153Comparing
the PowerShellDo...Untilstatement withVBScript.. .154Using
the Windows PowerShell Dostatement 154The For statement 156
Using
the For statement 156Using
the Foreachstatement 158Exiting
theForeachstatementearly
159 TheIfstatement 161Using assignment
andcomparison
operators 163Evaluating multiple
conditions 164The Switchstatement 164
Using
the Switch statement 165Controlling matching
behavior 167Creating multiple
folders:step-by-step
exercises 168Chapter
5quick
reference 170Chapter
6Working
with Functions
171
Understanding
functions 171Using
functionstoprovide
easeof codereuse 178Including
functions in the Windows PowerShell environment 180Using dot-sourcing
180Using
dot-sourced functions 182Adding help
for functions 184Using
ahere-string
objectforhelp
184Using
twoinput
parameters 186Using
atypeconstraint in a function 190Using
morethan twoinput
parameters 192Useof functionsto
encapsulate
businesslogic
194 Use of functionstoprovide
easeof modification 196Understanding
filters 201Creating
afunction:step-by-step
exercises 205Chapter
6quick
reference 208Chapter
7Creating
Advanced Functions and Modules
209 The[cmdletbinding]
attribute 209Easy
verbose messages 210Automatic parameterchecks 211
Adding
supportfor the-whatif
parameter 214Adding
supportforthe -confirm parameter 215Specifying
the defaultparameterset 216Theparameter attribute 217
The
mandatory
parameter property 217The
position
parameterproperty
218 The ParameterSetNameparameter property 219 TheValueFromPipeline
property 220 TheHelpMessage
property
221Understanding
modules 222Locating
andloading
modules 222Listing
available modules 223Loading
modules 225Installing
modules 227Creating
a per-user Modules folder 227Working
withthe $modulePathvariable 230Creating
a module drive 232Checking
for moduledependencies
234Using
a module from ashare 237Creating
a module 238Creating
anadvanced function:step-by-step
exercises 245Chapter
7quick
reference 249Chapter
8Using
the Windows PowerShell ISE
251Running theWindows PowerShell ISE 251
Navigating
the WindowsPowerShell ISE 252Working
with thescript
pane 254 Tabexpansion
and IntelliSense 256Working
withWindows PowerShellISEsnippets
257Using
Windows PowerShell ISEsnippets
to createcode 257Creating
newWindows PowerShell ISEsnippets
259Removing
user-definedWindowsPowerShell ISE snippets 261Using
the Commands add-on:step-by-step
exercises 262Chapter
8quick
reference 265Chapter
9Working
with WindowsPowerShell Profiles
267Six DifferentPowerShell
profiles
267Understanding
the six different Windows PowerShellprofiles
.. .268Examiningthe
$profile
variable 268Determining
whetheraspecific profile
exists 270Creating
a newprofile
270Design
considerations forprofiles
271Using
one or moreprofiles
273Using
the All Users, All Hostsprofile
275Using
yourownfile 276Grouping
similarfunctionality
intoa module 277Whereto storethe
profile
module 278Creating
aprofile: step-by-step
exercises 278Chapter
9quick
reference 282Chapter
10
Using
WMI
283Understanding
the WMI model 284Working
withobjects
and namespaces 284Listing
WMIproviders
289Working
with WMIclasses 289Querying
WMI 293Obtaining
service information:step-by-step
exercises 298Chapter
10quick
reference 305Chapter
11Querying
WMI 307Alternate waysto connect toWMI 307
Selective data from allinstances 316
Selecting multiple properties
316Choosing specific
instances 319Utilizing
anoperator 321Where is the where? 325
Shortening
thesyntax 325Working
withsoftware:step-by-step
exercises 327Chapter
11quick
reference 335Chapter
12
Remoting
WMI337
UsingWMI againstremotesystems 337
Supplying
alternate credentials for theremoteconnection 338Using
Windows PowerShell remotingtorunWMI 341Using
CIM classesto query WMI classes 343Working
withremoteresults 344Reducing
data via Windows PowerShellparameters
347Running
WMIjobs
350Using
Windows PowerShellremoting
andWMI:Step-by-step
exercises 352Chapter
12quick
reference 354Chapter
13Calling
WMIMethods
on WMIClasses
355
Using
WMI cmdletsto executeinstance methods 355Using
the terminate methoddirectly
357Using
the Invoke-WmiMethod cmdlet 358Using
the[wmi]
typeaccelerator 360Using
WMI towork withstatic methods 361Executing
instance methods:step-by-step
exercises 364Chapter
13quick
reference 366Chapter
14Using
the CIMCmdlets
367Using
the CIM cmdlets toexplore
WMI classes 367Using
the -classnameparameter 367Finding
WMIclass methods 368Filtering
classesby qualifier
369Retrieving
WMI instances 371Reducing returned
properties
and instances 372Cleaning
upoutput from the command 373Working
with associations 373Retrieving
WMI instances:step-by-step
exercises 379Chapter
14quick
reference 382Chapter
15Working
with Active
Directory
383Creating objects
in ActiveDirectory
383Creating
anOU 383ADSI
providers
385LDAPnames 387
Creating
users 393What isuseraccountcontrol? 396
Working
with users 397Creating multiple organizational
units:step-by-step
exercises 412Chapter
15quick
reference 418Chapter
16
Working
with the AD DS Module 419Understanding
the ActiveDirectory
module 419Installing
the ActiveDirectory
module 419Getting
started with the ActiveDirectory
module 421Using
the ActiveDirectory
module 421Finding
the FSMO role holders 422Discovering
ActiveDirectory
428Renaming
ActiveDirectory
sites 431Managing
users 432Creating
a user 435Finding
andunlocking
ActiveDirectory
useraccounts 436 xiv ContentsFinding
disabled users 438Finding
unuseduseraccounts 440Updating
ActiveDirectory objects:
step-by-step
exercises 443Chapter
16quick
reference 445Chapter
17Deploying
ActiveDirectory
with
Windows Server 2012
447
Using
the ActiveDirectory
moduletodeploy
a newforest 447Adding
a newdomain controllerto anexisting
domain 453Adding
aread-only
domain controller 455 Domain controllerprerequisites: step-by-step
exercises 457Chapter
17quick
reference 460Chapter
18
Debugging Scripts
461Understanding
debugging
inWindows PowerShell 461Understanding
three differenttypesoferrors 461Using
theSet-PSDebug
cmdlet 467Tracing
thescript
467Stepping through
thescript
471Enabling
strict mode 479Using Set-PSDebug
-Strict 479Using
the Set-StrictModecmdlet 481Debugging
thescript
483Setting breakpoints
483Setting
abreakpoint
on aline number 483Setting
abreakpoint
onavariable 485Setting
abreakpoint
on acommand 489Responding
tobreakpoints
490Listing breakpoints
492Enabling
anddisabling breakpoints
494Deleting breakpoints
494Chapter
19Handling
Errors
501Handling missing
parameters 501Creating
adefault value fora parameter 502Making
the parametermandatory
503Limiting
choices 504Using PromptForChoice
to limit selections 504Using
Test-Connection toidentify
computer
connectivity 506Using
the -containsoperatorto examinecontentsofanarray.. .507Using
the-containsoperatorto testfor properties 509Handling missing rights
512Attempt
and fail 512Checking
forrights
andexiting
gracefully
513Handling missing
WMIproviders
513Incorrectdata
types
523Out-of-boundserrors 526
Using a
boundary-checking
function 526Placing
limitsonthe parameter 528Using Try...Catch...Finally
529Catching multiple
errors 532Using PromptForChoice
to limit selections:Step-by-step
exercises 534Chapter
19quick
reference 537Chapter
20Managing Exchange
Server 539Exploring
theExchange
2010 cmdlets 539Working
with remoteExchange
servers 540Configuring recipient settings
544Creating
theuserandthe mailbox 544Reporting
usersettings
548Managing
storagesettings
550Examining
the mailbox database 550Managing
the mailboxdatabase 551Managing
Exchange logging
553Managing auditing
557Parsing
the auditXMLfile 562Creating
useraccounts:step-by-step
exercises 565Chapter
20quick
reference 570Appendix
AWindows PowerShell
CoreCmdlets
571
Appendix
BWindows PowerShell Module
Coverage
579Appendix
CWindows
PowerShell CmdletNaming
583Appendix
D Windows PowerShellFAQ
587Appendix
E Useful WMI Classes 597Appendix
F BasicTroubleshooting Tips
621
Appendix
GGeneral PowerShell
Scripting
Guidelines
625Index 633
About theAuthor 667
What do you think of this book? Wewant to hear from
you!
Microsoft is interested inhearingyourfeedbackso we cancontinually improveour books andlearningresourcesforyou.Toparticipateinabrief onlinesurvey,pleasevisit: