You are on page 1of 2

Windows PowerShell Quick Reference

How to &ccess &r'u$ents


To access command$line arguments used when starting a script use the automatic variable (ar's. !ou can cycle through the individual arguments in the $args collection by using code similar to this: .oreac$ /$i in $args1 2$i3 To access a particular argument use the collection index number, with 4 representing the first item in the collection, 5 representing the second item, etc: $args[0] !ou can reference the last item in a collection by using the index number 5: $args[-1]

Windows PowerShell Quick Reference


How to Read a Text File
To read the contents of a text file into a variable, call the Get-Content cmdlet followed by the path to the text file: $a = Get-Content C:\Scripts\Test.txt Each line in the file ends up as an item in the array $a. If you want to access a single line in the file you can simply specify the index number corresponding to that line: $a[0] This command echoes back the last line in $a: $a[-1] Bonus. To determine the number of lines, words, and characters in a text file use this command: get-content c:\scripts\test.txt | measure-ob ect -!ine -"or# -c$aracter

How to Solicit )n ut
To solicit input from a user, use the Read-Host cmdlet, followed by the prompt to be displayed: $a = :ea#-;ost -%!ease enter 7our name-

How to Write Conditional State$ents


To write an If statement use code similar to this: $a = -"$itei. /$a -e0 -re#-1 2-T$e co!or is re#.-3 e!sei. /$a -e0 -"$ite-1 2-T$e co!or is "$ite.-3 e!se 2-T$e co!or is b!ue.-3 Instead of writing a series of If statements you can use a #witch statement, which is e/uivalent to 01#cript2s #elect ,ase statement: $a = 4 s"itc$ /$a1 2 1 2-T$e 4 2-T$e 5 2-T$e 6 2-T$e #e.au!t 3

How to )nsert %ine Breaks


To insert a line break into a *indows +ower#hell script use the backtick '6) : <rite-;ost = -T$is is a continuation o. t$e !ine.!ou can also break a line at the pipe separator '7) character 'assuming your line uses the pipeline): Get-C$i!#>tem C:\Scripts | Sort-&b ect ?engt$ (@escen#ing

How to *se Colored Text


To display text in a different color use the Write-Host cmdlet and specify a foreground color: <rite-;ost -test- -.oregroun#co!or -green!ou can also specify a different background color: <rite-;ost -test- -bacAgroun#co!or -re#-

How to Create +ulti-Co$$and %ines


To put multiple commands on a single line, separate those commands using a semicolon: $a = 1C4C5C6CD8 $b = $a[4]8 <rite-;ost $b

co!or is re#.-3 co!or is b!ue.-3 co!or is green.-3 co!or is 7e!!o".-3 2-&t$er.-3

How to Write to a Text File


To save data to a text file use the Out-File cmdlet: Get-%rocess | &ut-'i!e C:\Scripts\Test.txt To append data to an existing file, add the a parameter: end

How to Write For and For !ach %oo s


To write a 3or statement use code similar to this: .or /$a = 18 $a -!e 108 $a991 2$a3 1y comparison, a 3or Each statement might look like this: .oreac$ /$i in get-c$i!#item c:\scripts1 2$i.extension3

How to +ake Co$ arisons How to )nsert a Para'ra h Return


To insert a paragraph return in your output use the newline character /n: <rite-;ost -?ine 1.=n?ine 4.*indows +ower#hell cmdlets 'like Where-O,-ect) use a special set of comparison operators, including those shown in the following table. Each of these operators can be made case sensitive by adding a c immediately after the hyphen. 3or example, -ce. represents the case$sensitive e/uals operator8 -clt is the case$sensitive less than operator. -lt -le -'t -'e -e. -ne -like -notlike 9ess than 9ess than or e/ual to :reater than :reater than or e/ual to E/ual to ;ot e/ual to 9ike 'uses wildcards for matching) ;ot like 'uses wildcards for matching)

Get-%rocess | &ut-'i!e C:\Test.txt (appen# !ou can also use the "#$%&# redirection characters '( for write, (( for append) when using *indows +ower#hell. This command writes data to the file ,:-#cripts-Test.txt: Get-%rocess ) C:\Scripts\Test.txt .nother option is to use the !x ort-CS" cmdlet to save data as a comma$separated$values file: Get-%rocess | *xport-CS+ C:\Test.cs,

How to Write in Re0erse "ideo


To echo a message in reverse video use the WriteWarnin' cmdlet: <rite-<arning -Bn error $as occurre#.-

How to Write #o %oo s


To write a %o loop use code like the following, replacing the code between the curly braces with the code to be executed on each iteration of the loop. &h: and replacing the code inside the parentheses with the loop condition: $a = 1 #o 2$a8 $a993 "$i!e /$a -!t 101 $a = 1 #o 2$a8 $a993 unti! /$a (gt 101

How to )nsert Co$$ents


To insert a comment, use the pound sign '<): E T$is is a commentC not a !ine to be run.

How to Print #ata


To print data to the default printer use the Out-Printer cmdlet: Get-%rocess | &ut-%rinter

Windows PowerShell Quick Reference


How to Create a CO+ O,-ect
To work with a ,&" ob?ect use the 4ew-O,-ect cmdlet followed by the co$o,-ect parameter and the appropriate +rogI%: $a = He"-&b ect -comob ect = -*xce!.Bpp!ication$a.+isib!e = $True

Windows PowerShell Quick Reference


How to Get Hel
To get complete help information for a *indows +ower#hell cmdlet, use the Get-Hel cmdlet along with the full parameter. 3or example, to view the help information for the :et$+rocess cmdlet type the following: Get-;e!p Get-%rocess (.u!! To view the example commands for a cmdlet use the exa$ les parameter: Get-;e!p Get-%rocess (examp!es If you can2t remember the exact name for a cmdlet use :et$,ommand to retrieve a list of all the cmdlets available to you: Get-Comman# 3or a list of available aliases, use the :et$.lias cmdlet: Get-B!ias

How to Work with W+)


To get computer information using *"I call the GetW+)O,-ect cmdlet followed by the class name: Get-<G>&b ect <in54IJ>&S If the class you are interested in does not reside in the cimvE namespace simply include the na$es ace parameter: Get-<G>&b ect S7stem:estore = -namespace root\#e.au!t To access data on another computer use the co$ uterna$e parameter: Get-<G>&b ect <in54IJ>&S = (computername at!-"s-01 To limit returned data, use a *G9 /uery and the .uer1 parameter: Get-<G>&b ect -0uer7 = -Se!ect K 'rom <in54ISer,ice = <$ere State = LStoppe#L-

How to Co 1 and Paste


To enable simple copying and pasting in the *indows +ower#hell console do the following: #tart *indows +ower#hell, then click the icon in the upper left$hand corner and choose Pro erties. In the Windows PowerShell Pro erties dialog box, on the O tions tab, select Quick!dit +ode and then click &=. To copy text in the console window select the text and then press E;TE>. To paste text into the window click the right mouse button.

How to Create a 54!T O,-ect


To instantiate and use a .;ET 3ramework ob?ect enclose the class name in s/uare brackets, then separate the class name and the method using a pair of colons: [s7stem.Het.@HS]::reso!,e/-40N.6O.1PQ.50-1 To create an ob?ect reference to a .;ET 3ramework ob?ect use the 4ew-O,-ect cmdlet: $a = ne"-ob ect = -t7pe s7stem.#iagnostics.e,ent!og = -argument!ist s7stem 4ote. This is a cursory overview of working with .;ET. The two techni/ues shown here will not necessarily work with all .;ET classes.

How to Run a Scri t


To run a script from within *indows +ower#hell, type the full path to the script 'or type the script name if the script is stored in a folder that is part of your *indows path): C:\Scripts\Test.ps1 If the path name includes blank spaces you must preface the path with an ampersand and enclose the path in double /uotes. 3or example: F-C:\Scripts\G7 Scripts\test.ps13rom outside *indows +ower#hell 'e.g., from the Run dialog box or from a ,md.exe window) you must call *indows +ower#hell and then pass the script path as an argument to that call: po"ers$e!!.exe (noexit C:\Scripts\Test.ps1 The -noexit parameter ensures that the +ower#hell window remains open after the script finishes running.

How to Chan'e Securit1 Settin's How to Bind to &cti0e #irector1


To bind to an .ctive %irectory account use the 9%.+ provider: $a = [a#si] -?@B%:MMcn=Aenm7erC = ou='inanceC #c=.abriAamC #c=com9isting all the ob?ects in an &H is a little more complicated8 however, one relatively easy way to accomplish this task is to bind to the &H and then use the PSBase6GetChildren78 method to retrieve a collection of items stored in that &H: $ob &R = [B@S>]= -?@B%:MMou='inanceC#c=.abriAamC#c=com$users = $ob &R.%SJase.GetIC$i!#ren/1 $users | Se!ect-&b ect #isp!a7Hame To run scripts from within *indows +ower#hell you will need to change your security settings8 by default, +ower#hell only runs scripts signed by a trusted authority. To enable +ower#hell to run all locally$created scripts 'regardless of whether or not they have been signed) use the following command: Set-*xecution%o!ic7 :emoteSigne#

How to Select Pro erties


To work with or display specified properties of a collection, pipe the returned results to the Select-O,-ect cmdlet: Get-%rocess | Se!ect-&b ect HameC Compan7

How to Sort #ata


To sort data returned by *indows +ower#hell simply pipe that data to the Sort-O,-ect cmdlet, specifying the property you want to sort by: Get-%rocess | Sort-&b ect >@ !ou can also add the descendin' or -ascendin' parameters to specify a sort order: Get-%rocess | Sort-&b ect >@ (#escen#ing !ou can even sort by multiple properties: Get-%rocess | Sort-&b ect %rocessHameC >@

How to 2)nterro'ate3 an O,-ect


To get information about the properties and methods of an ob?ect retrieve an instance of that ob?ect and then @pipeA the ob?ect to the Get-+e$,er cmdlet. 3or example, this command returns the properties and methods available when working with processes: Get-%rocess | Get-Gember

How to Get +ore )nfor$ation


3or more information on writing *indows +ower#hell scripts visit the Tech;et #cript ,enter at http:BBtechnet.microsoft.comBen$ usBscriptcenterBddCDED5F.aspx.

How to Bind to %ocal &ccounts


To bind to a local account, use the *in;T provider: $a = [a#si] -<inHT:MMat!-"s-01MAenm7er$a.'u!!Hame

How to Clear the Console Window


To clear the +ower#hell window, use the Clear-Host function 'or its alias, cls).

You might also like