Professional Documents
Culture Documents
This is your guide to getting started with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon youll be driving around like a pro. On This Page Getting Started with Windows PowerShell Starting Windows PowerShell Whoa, Whoa, Wait a Second: What Are Cmdlets? I Have a Couple Questions For You PowerShell is Here to Help Hey, What About Scripts? So Where Do I Go From Here?
mode of system management for something new, something that you might not like, something that might be too hard? Those are all good questions and all valid concerns. And, to be honest, we cant really address them all in this introductory article. However, we will say this: just because you get a drivers license that doesnt mean that you can never ride a bike again. Transportation is not a zerosum proposition: having a drivers license doesnt mean you cant also ride a bike, a bus, or a motorcycle. (Or even - gasp! - walk.) Would you rather ride a bike than drive a car? Thats fine; ride a bike when you want to ride a bike, and drive a car when you want to drive a car. If it turns out that youre happier riding a bike, well, good for you: ride your bike, and drive a car only when you really need to. You should think of Windows PowerShell in the same light. If you learn Windows PowerShell, does that mean you have to stop using the GUI and you have to throw out all your VBScript scripts? Of course not. Many people believe that Windows PowerShell is a replacement for VBScript and other management technologies. Thats not really true; if anything, PowerShell is a complement to VBScript and other management technologies. Over time, some people will decide, Wow, I really like PowerShell; I think Im going to use PowerShell for everything. Thats fine; have fun and let the Scripting Guys know how we can help. Other people are going to think, I dont know. I like some of the things PowerShell has to offer, but Ive got a pretty big investment in VBScript scripts. Id rather stick with VBScript. Well, guess what? Thats also fine. As the saying goes, if it aint broke, dont fix it. If VBScript works for you then keep using VBScript. So then why even bother with Windows PowerShell? There are several reasons. For one, its like having a drivers license: you might never need it, but its good to have, just in case. Its true that, at the moment, PowerShell might not seem like that big of a deal: for the average system administrator pretty much everything you can do with Windows PowerShell you can already do with VBScript. On the other hand, however, there are exceptions: if you want to automate the management of Microsoft Exchange Server 2007 youll need to use PowerShell. As time marches on these exceptions are likely to become the rule: PowerShell will continue to be enhanced and improved upon (version 2.0 is in development right now) while VBScript wont. Admittedly, you might not need PowerShell right now; however that could change in the very near future. So why not go ahead and learn it now, when you can take your time and learn at your pace? That, of course, is the reason for the Windows PowerShell Owners Manual: this is intended to be a gentle and relaxed introduction to Windows PowerShell. Like most owners manuals, well tell you how to put the pedal to the metal and drive your new command console and scripting language; over time, well also explain how to care for and maintain your new command console and scripting language. And dont worry: in addition to standard equipment well cover accessories and customization as well. We know that some of you installed Windows PowerShell and, ever since, have been sitting around thinking, Well, now what? If thats you, well, you can stop fretting: youve come to the right place.
Note. For better or worse, this particular article can only provide a somewhat-cursory introduction to Windows PowerShell. As time goes by, well drill down deeper and deeper into PowerShell and what you can do with it; if you cant wait till then you might want to take a look at the Scripting Guys five Windows PowerShell webcasts.
Top of page
If youre thinking, Hmmm, that looks a lot like a good old MS-DOS command window, well, theres a reason for that: PowerShell actually leverages the good old MS-DOS command shell. And theres at least one advantage to that. Because youve probably used the old MS-DOS command shell a million times you already have a pretty good idea of how to use PowerShell: you just type in commands and press ENTER. For example, do you need to know your IP address? Then typeipconfig and press ENTER:
Ethernet adapter Local Area Connection: Media State . . . . . . . . . . . : Media disconnected Ethernet adapter Wireless Network Connection: Connection-specific IP Address. . . . . Subnet Mask . . . . Default Gateway . . DNS . . . . . . Suffix . . . . . . . . . . . . . . . . : : : : fabrikam.com 192.168.120.17 255.255.255.0 192.168.120.1
As the preceding example implies, you can run all your command-line executables from within the PowerShell console; you can even run VBScript scripts and batch files from within the
PowerShell console. Want to run the script C:\Scripts\Test.vbs from within Windows PowerShell? Okey-doke:
PS C:\Scripts> test.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. This message was generated by Test.vbs.
Note. Interested in learning more about the console itself? Then take a look at the Owners Manual sections on customizing the PowerShell console and onWindows PowerShell shortcut keys. Oh, and dont forget tab expansion.
In addition, a lot of the console commands youve grown to love - like cd and cls, or even Unix commands like ls - have Windows PowerShell equivalents. Although there are some minor exceptions, these commands work exactly the way you would expect them to work. Want to change from the C:\Scripts folder to the C:\Windows folder? Then just use the cd command:
PS C:\scripts> cd c:\windows PS C:\WINDOWS> Or, if you prefer: PS C:\scripts> chdir c:\windows PS C:\WINDOWS>
In general, these console commands are aliases to Windows PowerShell cmdlets. If youd like to see a complete list of available aliases just type the following from the PowerShell command prompt and press ENTER:
Get-Alias
In case youre wondering, PowerShell commands are typically case-insensitive. You could also type this from the command prompt:
get-alias
Or even this:
Of course, Windows PowerShell includes cmdlets that do things far more interesting than simply switch you from one folder to another. For example, try typing this command and seeing what you get back:
PS C:\WINDOWS> Get-Process
You should get back information similar to this, information about all the processes currently running on your computer:
NPM(K) -----5 7 4 7 5 13 7 6
WS(K) VM(M) ----- ----3676 32 10244 70 4692 38 6968 54 6640 52 20256 77 6864 68 4544 38
Pretty slick, huh? Maybe youd like to list all the files and folders in C:\Scripts, including anything which happens to live in a subfolder of C:\Scripts. Well, all you had to do was ask:
All were doing here is calling the Get-ChildItem cmdlet. You might have noticed that we also passed Get-ChildItem a pair of parameters: C:\Scripts and -recurse. As you probably figured out, C:\Scripts is the name of the folder we want to work with. You might notice, as well, that we didnt enclose the folder path in double quote marks. Thats pretty much always going to be true in PowerShell: theres no need to enclose parameters in quote marks unless that parameter includes blank spaces. For example, suppose you want to list the files and folders found in C:\Documents and Settings\Ken Myer. Because that path includes blank spaces you need to enclose the path name in double quote marks:
The second parameter in our original command, -recurse, tells the Get-ChildItem to recursively retrieve information from the parent folder; thats just a fancy way of telling Get-ChildItem to retrieve information from all the subfolders of C:\Scripts (as well as sub-subfolders of those subfolders, and so on). Windows PowerShell version 1.0 ships with 129 cmdlets, cmdlets that let you work with files and folders, read from and write to text files, manage event logs, and even instantiate COM and .NET Framework objects. Now thats all well and good, but the Scripting Son didnt really learn how to drive until he got behind the wheel and started driving: reading about something is no substitute for actually doing something. Windows PowerShell is no different: you can read about cmdlets all you want, but you wont get a good feeling for cmdlets and what they can do until you get behind the wheel and actually start using Windows PowerShell. And while the Scripting Guys cant sit in the passengers seat and offer useful advice (like, Slow down! Watch out for the curb! Dont you ever slam on the brakes like that!) we can do the next best thing: we can provide step-by-step guidance that walks you through a typical system administration scenario using Windows PowerShell. If that sounds like fun (and why wouldnt it be?), take a look at our Windows PowerShell virtual lab. Bonus Tip Heres a little time-saving tip for you. When using parameters in conjunction with a cmdlet, you only have to type as much of the parameter name as needed to make that parameter distinct. What does that mean? Well, Get-ChildItem has only one parameter (recurse) that starts with the letter r. That means we can specify the -recurse parameter by using this command:
Get-ChildItem C:\Scripts -r
Pretend, for a moment, that Get-ChildItem has another parameter that starts with the letter r; lets call this hypothetical parameter -readonly. In that case we can specify the -recurse parameter by using this command:
Why did we have to type -rec? Thats easy: we had to type three letters before we could differentiate -recurse and -readonly: -recurse -readonly
Still too much typing for you? Well, if you use the gci alias (which, needless to say, is the alias for Get-ChildItem) you can retrieve a recursive list of all the files and folders in C:\Scripts by doing no more typing than this:
gci C:\Scripts -r
PS C:\scripts> Get-Command CommandType Name n -------------Cmdlet Add-Content nt [-Path] <String[]> [-Value] <Object[... Cmdlet Add-History ry [[-InputObject] <PSObject[]>] [-Pass... Definitio --------Add-Conte Add-Histo
Cmdlet Add-Member r [-MemberType] <PSMemberTypes> [-Name]... Cmdlet Add-PSSnapin pin [-Name] <String[]> [-PassThru] [-Ve... Cmdlet Clear-Content tent [-Path] <String[]> [-Filter <Strin... Cmdlet Clear-Item m [-Path] <String[]> [-Force] [-Filter ... Cmdlet Clear-ItemProperty mProperty [-Path] <String[]> [-Name] <S...
A Quick Detour What do the three dots () at the end of each line mean? They mean that there is additional information about each cmdlet; however, that information wouldnt all fit on the screen. Is there a way to make it all fit on the screen? Sure; all you have to do is display the information in list view rather than table view:
Name : Add-Content CommandType : Cmdlet Definition : Add-Content [-Path] <String[]> [-Value] <Object[]> [-PassT hru] [-Filter <String>] [-Include <String[ ]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredent ial>] [-Verbose] [-Debug] [-ErrorAction <A ctionPreference>] [-ErrorVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [ -Confirm] [-Encoding <FileSystemCmdletProviderEncoding>] Add-Content [-LiteralPath] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include < String[]>] [-Exclude <String[]>] [-Force] [-Credential <PS Credential>] [-Verbose] [-Debug] [-ErrorAc tion <ActionPreference>] [-ErrorVariable <String>] [-OutVa riable <String>] [-OutBuffer <Int32>] [-Wh atIf] [-Confirm] [-Encoding <FileSystemCmdletProviderEncod ing>] Path : AssemblyInfo : DLL : C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.Commands .Management\1.0.0.0__31bf3856ad364e35\Micr osoft.PowerShell.Commands.Management.dll HelpFile : Microsoft.PowerShell.Commands.Management.dll-Help.xml ParameterSets : {Path, LiteralPath} ImplementingType : Microsoft.PowerShell.Commands.AddContentCommand Verb : Add Noun : Content
How did we manage to do that? Well, as it turns out, Format-List is a cmdlet that takes information passed to it and then outputs that information in list view (that is, property values are displayed on separate lines rather than as columns in a table). But whats that weird symbol | - in the middle of the command? Well, that weird symbol is the pipe separator, and it indicates that we want to pipe the information returned by Get-Command and hand it over to FormatList for additional processing. You say that makes no sense to you whatsoever? Thats all right; thats why this manual includes a chapter on pipelines and piping. Now, Back to Our Regularly-Scheduled Article Get-Command is pretty useful: after all, it gives you a definitive list of the cmdlets that are available to you. However, it doesnt tell you how to use those cmdlets. For that, you need to access PowerShells built-in help system. Top of page
Get-Help Format-List
Wait, wait: dont type that in yet. Like we said, PowerShell has a very extensive help system; however, it also has a somewhat complicated help system. Typing Get-Help Format-List will, indeed, show you additional information about the Format-List cmdlet; however, that information will consist largely of a syntax diagram and a brief description of the cmdlet. For more extension information, including example commands and a detailed description of allowed parameters, you need to tack on the -full parameter, like so:
Or, if youre interested only in the example, tack on the -examples parameter:
Wait, wait: dont type those commands in yet, either. The actual help topics found in PowerShell are very, very good; however, PowerShells help engine leaves a little something to be desired. For example, when you open a help topic using Get-Help, PowerShell displays the entire topic and then leaves the cursor at the very end of the topic. To actually read the information from
start to finish you have to scroll up and find the first line. You can work around that problem (sort of) by piping the results of Get-Help to the more command, which enables you to display information one page (screen) at a time:
That helps, although you can only page your way forward; you cant go backwards. (Try it and youll see what we mean.) The other issue is a little harder to work around: for better or worse (mostly for worse) PowerShells help engine doesnt do a very good job of respecting word breaks at the end of a line. For example:
DETAILED DESCRIPTION The Get-Process cmdlet retrieves a process object for each process. Witho ut parameters, "Get-Process" gets all of t he processes on the computer, as though you typed "Get-Process *". You ca n also identify a particular process by pr ocess name or process ID (PID), or pass a process object through the pipe line to Get-Process. For Get-Process, the default method is by process name. For Stop-Process, the default method i s by process ID.
Take a look at the first line under Detailed Description. As you can see, the word the doesnt quite fit on the line. However, instead moving the entire word to the next line, PowerShell went ahead and printed the t at the end of line 1, then printed the rest of the word (he) at the beginning of line 2. Does that make the help a little hard to read? You might say that. So is there a workaround for this word-wrap issue? Not as far as we know. Which is why the Scripting Guys put together the Windows PowerShell graphical help file. This is the same help you get in Windows PowerShell (plus, just for the heck of it, a copy of our VBScript to Windows PowerShell Conversion Guide). However, weve taken that help and wrapped it up in a regular old .chm file; that makes it a little easier to read, and gives you full-text search capability as well. You can even access this Help from the command prompt, simply by typing a command similar to this:
Get-GUIHelp Format-List
And no need for parameters like -full or -examples. With the graphical help file you always get all the information available for a topic. But you know what? Youre right. Help is incredibly useful, provided that you already know what it is you want to do. (For example, you know you want to use Get-ChildItem to list all the files and folders found in C:\Scripts, you just cant remember which parameter enables you to
recursively search for these files.) But what if you have no idea what you can even do with Windows PowerShell in the first place? Are you just supposed to read the help file from start to finish? Well, you can if you want to. Alternatively, you might want to take a look at the Script Centers Task-Based Introduction to Windows PowerShell. Here youll find scores of taskbased articles, with titles like Save Data to a Text File and Copy Files or Folders. One thing you might want to do is browse through those articles and then look in the help file for more detailed information on the cmdlets used to carry out those tasks.
Note. And yes, the graphical help file includes links to corresponding articles in the Task-Based Introduction. For example, if youre viewing information about the Set-Content cmdlet in the graphical help file youll see a link to the article Save Data to a Text File.
Top of page
Note. OK, there might be a little more: you actually have to write some script code. Yet another reason to take a look at the PowerShell scripting webcast.
Heres another cool thing about PowerShell. Throughout this article weve referred to PowerShell as being a command console and scripting language. Theres a good reason for that: you can run PowerShell commands from the command prompt or you can save those commands as .ps1 files and run them as scripts. Even cooler, these commands can be (and usually are) identical: the commands you type at the command prompt can be used as-is in scripts, and vice-versa. For example, consider this simple two-line PowerShell script:
$a = Get-ChildItem C:\Scripts
$a
In the first line, this script uses Get-ChildItem to retrieve a collection of all the files and folders stored in C:\Scripts; that collection is stored in the variable $a. In the second line, the script simply displays the value of $a. Not all that exciting, is it? Thats hard to argue with. However, what is exciting is this: you can either save these two commands as a .ps1 file and then run those commands as a script, or you can type the commands at the command prompt, one after another. What happens if you type the commands at the command prompt? You got: the first command retrieves a collection of all the files and folders stored in C:\Scripts and stores that information in a variable named $a; the second command echoes back the value of $a. Work from the command prompt or write a script? In a certain sense, it doesnt really matter. Needless to say, well have more - much more - about scripts and script writing in future updates to the Owners Manual. Top of page
Second, kick the metaphorical PowerShell tires by trying out the virtual lab. Its always better to try something hands-on rather than read about it. (Which, you might have noticed, we didnt bother to tell you until youd finished reading this big, long article.) And keep visiting the Script Centers PowerShell section. Well be adding more and more content to this section, including more installments to the Owners Manual.