Learn Windows PowerShell Fast Crash Course
- select the contributor at the end of the page -
There are, of course, numerous ways to automate various administrative tasks. Augmenting the GUI-based managers is an array of management tools including profiles, login scripts, batch files, vbscripts, and other tools that enable certain things to be taken care of automatically. Unfortunately, there are limitations to these tools, not the least of which is, that while they can be effectively deployed to automate many recurring tasks, they are too clunky to handle those large management tasks that do not occur on a regular basis. Login scripts run only at login, not whenever desired. Profiles must be attached to previously created groups or collections or new groupings need to be created for each
To fill the gaps between these various tools, and to move Microsoft systems administrators from the labor intensive world of point and click management and catch-all login scripts, Microsoft created a powerful scripting environment named PowerShell. For real in-depth PowerShell training, you'll need a good course, but an experienced administrator can learn PowerShell basics fast enough to be running scripts in no time.
There are two ways to use PowerShell scripts. There is an interactive scripting session window that allows an administrator to enter and run scripts directly, much like entering commands at a DOS prompt. A command is entered and then immediately executed.
Scripts can also be created, saved and then called and executed at a later time. This is not unlike creating a batch file or vbscript and then running it. PowerShell scripts are plain text files and have a PS1 extension. These scripts can then be executed on any system with PowerShell installed, which includes all recent Windows Server systems and most updated Windows desktop operating systems including XP, Vista and Windows 7. The Windows 8 developer preview even includes a new PowerShell 3 version.
If you have an updated Windows operating system (desktop or server), chances are that Windows PowerShell is already installed and waiting on your computer:
Click Start -> (All) Programs -> Accessories -> Windows PowerShell
The Windows PowerShell ISE is the Integrated Scripting Environment window, while the Windows Powershell menu item launches a command window where scripts run immediately.
The commands in Windows PowerShell are known as cmdlets. Each cmdlet is a single function command-line based tool. Cmdlets names are NOT case sensitive, so save your shift key.
Quick PowerShell Tutorial
The fastest and easiest way to use PowerShell is to type commands directly into the PowerShell ISE window. By default, the ISE interface is divided into three parts. The top is a text editor used for creating multistep scripts to be saved as .ps1 files. We'll cover using this later. The middle section is where commands are executed and any response or return codes are displayed. Finally, the bottom section is an interactive script prompt, similar to the familiar command prompt so many administrators are already familiar with. To dive right in and run PowerShell immediately, this is the pane you type commands into.
For example, let's say that you want to stop a service using PowerShell. First, you have to know what cmdlet to use. Like all programming languages, PowerShell has hundreds of commands (thousands, if you include all the possible add-ins). You won't memorize them all, but you'll want to get used to the cmdlets that you use frequently. Look up the others as you need them.
PowerShell has a robust help file system, in part because the only other real documentation exists in TechNet. The PowerShell help system is very useful once you get to know it. Fortunately, it is easy to figure out.
Type help in the interactive command pane at the bottom of the ISE.
The actual help cmdlet is called Get-Help (remember, caps don't matter), but there is an alias so that just typing "help" returns the actual help entry for the Get-Help command. You'll notice the Get-Help help entry appears in the middle pane of the ISE, like all command execution. To get more room to read, you can drag the dividers between the panes, or just click the little ^ above the top pane to close the scripting window. Drag the command pane on the bottom to make it smaller as well, after all, you'll only be entering one command at a time.
Take a look at the help entry and you'll notice it is likely very familiar if you have ever used any programming help feature. The "Topic" is the name of the cmdlet you are currently looking at. The short description, is just what it says. The Long Description is where you get the information you need to run the cmdlet. In particular, we need to see the information under SYNTAX where you see what parameters the cmdlet can take.
The best way to use the help file is to just take a stab at what you are looking for and see where it gets you. Many times, you'll get exactly the information you need on the first try. The other times you'll get a pretty good hint where to go.
Since we want to stop a service, let's try to find the cmdlet we need.
Try typing get-help services.
In the middle pane a list of help topics and cmdlets related to services appears. About half way down there are Stop-Service, Start-Service, Suspend-Service, Resume-Service and Restart-Service. Those all look promising. The next step is to check the help file on one of those.
The help entry tells us how to stop a service. Under syntax there are several parameters that can be used, showing how this command can be used to do more than just stop a service. Before going any further, there are a couple of things to note. First, under RELATED LINKS is an "online version" of this help file. If things ever get too unwieldy, or if you just want to take advantage of clicking and opening tabs to keep find more information, you can copy and paste the URL into your browser and research from there. Also, at the bottom of the entry are REMARKS which points out three options that exist for most cmdlet help entries: -examples, -detailed, and –full.
In this case, you might be wondering, for example, what the –Force or –PassThru parameters mean. You will find them explained in depth by using the –detailed switch. By the way, the detailed information also displays the examples as part of the entry, so if you save a step by just using the detailed parameter if you need both pieces of information.
Now, we're ready to stop a service. Let's try stopping the Spooler service.
Type "stop-service spooler"
It didn't work, did it?
In red, you'll see the error message that was returned by running our command. In this case, the Spooler service has dependent services. That means we can only stop the Spooler service if we either stop all the dependent services first, or if we use the Force flag.
Type "stop-service spooler –force"
(If you get an error here about not being able to open the service, it is because you did not run the ISE as an administrator.)
PowerShell works on the theory of no news is good news. After executing the stop-service cmdlet with the force switch, the ISE show… nothing. That means it worked. You can go into the Services administrator tool and verify it if you like.s
That's all there is to running simple PowerShell commands. Of course, this is a trivial example. For this amount of effort you might as well just have used the Services tool. But, there is real power lurking just one level deeper. We'll look at that next time.