Powershell is a new scripting environment for Windows. It is a free add-on for Windows XP SP2, Server 2003, Vista or Server 2008. Microsoft created Powershell as part of a strategy for making Windows more command-line friendly, though it is unfortunate that it does not run on Server Core, the new installation option for Windows Server that has no GUI at all.
Powershell is also an interesting language in its own right. It is based on .Net, is fully object oriented, and designed to be powerful and concise. Although it is particularly useful for administrators, it can be used for all kinds of task. Powershell is addictive.
A core concept in Powershell is the pipeline. This lets you pass the output from one part of a statement as input to the next part, avoiding the need for intermediate variables. For example, when investigating performance in Windows, you might run Task Manager and sort the running processes by memory or CPU usage. Here’s a single-line command in Powershell that shows the top 10 processes by memory usage, with the results shown in screen 1:
get-process | sort-object WS - descending | select-object - first 10
Note the use of the vertical bar, which is the pipe character. You can read this as a flow:
1. Get a collection of objects representing running processes.
2. Take this output and sort it into a new collection, sorted by working set
(memory).
3. Take this output and select the first 10.
Powershell is easily extended, and people have been busy posting their most
useful or fun scripts. One collection is called the PowerShell Community
Extensions. This includes a script, or CmdLet in PowerShell jargon, which
converts text to speech. Once installed, tag the following to the end of a
statement, and the output is read aloud instead of printed:
| out-speech
That could be handy for someone with failing eyesight and shows the flexibility of the tool. What follows is not a complete tutorial, but some hints to get you started, plus a sample script.
Powershell survival guide
To install Powershell, just download it and run setup. You will then find it on
the Start menu. Think of it as an alternative to the traditional command prompt.
It supports ancient Dos commands such as DIR, as well as Unix-like commands such
as ls and its own more object-oriented techniques.
There is no code completion in PowerShell, so to find out what properties and methods an object supports use the get-member command. Try it on a process object:
get-process | select-object - first 1 | get-member
Powershell is annoying in that it installs by default with a restrictive execution policy, requiring all scripts to be signed. Unless this is what you want, change it to allow local scripts. To do this, run PowerShell with local administrator rights at least once. On Vista, right-click and choose Run as Administrator, then type:
Set-ExecutionPolicy RemoteSigned
This means local scripts, those you write yourself, will run without being signed. Scripts that are downloaded will not run until you unblock them: right-click, Properties, Unblock.
All Software Applications Tags: Visual Programming
