I first wrote about Windows PowerShell, Microsoft's new shell interface for managing Windows from the command line,...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
in my October 2006 column. Historically, Microsoft has not supported managing Windows from the command line. Yes, Windows has always had a set of command-line tools, and tools like VBScript have been available, but when it comes to performing complex tasks, or tasks not previously anticipated by the system designers, Windows' command line performance couldn't compare to its graphical performance.
Why? For starters, the command line was inconsistent. For example, performing a task with one tool didn't guarantee that the knowledge could be applied elsewhere since the syntax and structure were not fixed.
How PowerShell differs from other languages
But Microsoft eventually developed a shell language that was more flexible than its predecessors. This language, called Windows PowerShell, is more succinct than the interpreted languages people had been using for administrative scripting. It also adds support for .NET.
With Windows PowerShell, you can access many different kinds of objects in the OS. It also has many objects and commands built into it, and once you gain expertise in using it, you can add more if you find it lacking. And since it is a shell, users can test out commands on the fly in a way that is not possible with VBScript.
We've been discussing VBScript for two years, but now that Microsoft has finally released Windows PowerShell, it's time to download PowerShell and see what this more flexible tool can do. Make sure you choose the right version. Also, if you don't already have .NET 2.0 installed on your computer, you'll need to get that as well.
Now that you have access to PowerShell, let's take a closer look at how it works.
About Cmdlets: PowerShell's core instructions
Cmdlets are the core instructions built into PowerShell, aggregated collections of functions that you're likely to want that have been given an easy-to-use title.
Once PowerShell is installed, start it. You'll open a new command window that looks a bit like the regular command line. Forget about "Hello, World"-- the first thing you're going to do with PowerShell is get the status of all the services on your computer.
In VBScript, this is not difficult but takes a bit of typing and talking to Windows Management Instrumentation (WMI). From the Windows command prompt, close the command prompt and open the Services tool because the command prompt doesn't supply this kind of information. In PowerShell, enumerating all the services on the local computer looks like:
Note: How did I know that? I typed Get-Command. On its own, this cmdlet returns a list of all cmdlets and a short look at their syntax. In combination with any cmdlet, Get-command gets you help for that cmdlet.
Type that, and you'll spit out a list of the services on your computer, their status, and their display names, like this partial list:
Stopped AppMgmt Application Management Stopped aspnet_state ASP.NET State Service Running Ati HotKey Poller Ati HotKey Poller Running AudioSrv Windows Audio Running BITS Background Intelligent Transfer Ser... Running Browser Computer Browser
This works because PowerShell anticipates that it would be good to be able to see a list of services and their status, and so creates a cmdlet (a built-in function, in VBScript) that creates this list. But it doesn't stop there.
I have VMware currently running my computer. Frankly I don't need all its services to be on, since I'm not running any virtual machines, and I'd like to save the memory. So let's find and stop all the VMware-based services. To find them, I'll use the command below, which tells Get-Services to return only the services beginning with VM.
Note: As you can see, PowerShell takes wildcards -- a big improvement over VBScript!
That command returns this output:
Status Name DisplayName ------ ---- ----------- Running VMAuthdService VMware Authorization Service Running VMnetDHCP VMware DHCP Service Running vmount2 VMware Virtual Mount Manager Extended Running VMware NAT Service VMware NAT Service
While it seems I managed to avoid any non-VMware services, what if I'm not sure that I got all the VMware-related services? I'll use the command below to search by display name, since "VMware" appears in every display name.
Get-Service -displayname *VMware*
I receive the same output -- indicating that I've found all such services.
Now let's stop these services. This calls for a new cmdlet: Stop-Service. I could use Stop-service on its own, but I can also combine it with the command I used to find all the VMware-based services, by separating the two halves of the command with the pipe operator. Note that I'm performing one command—"get-service"— then sending its output, through the pipe operator, to "stop-service".
get-service -displayname *VMware* | stop-service
PowerShell shows the progress as these services stops, then returns to the prompt. Now, when I run Get-Service all those services are stopped. And if I want to start my VMs again, I enter:
get-service -displayname *VMware* | start-service
As you can see, with PowerShell you can create a simple one-line command to do something that would take multiple commands in VBScript.
Read all of Christa Anderson's scripting columns on SearchWinComputing.com.
|ABOUT THE AUTHOR:|
| Christa Anderson
A Terminal Services MVP, Christa Anderson is the strategic technology manager for visionapp She formerly was program manager for the Microsoft Terminal Services team. She is an internationally known authority on scripting, the author of Windows Terminal Services, The Definitive Guide to MetaFrame XP, and co-author of the book Mastering Windows 2003 Server. If you have a scripting question for Christa, please email her at editor@SearchWincomputing.com. She often uses these emails as fodder for her scripting columns.