image: Sorting processes
Sorting processes by memory usage
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Windows scripting environment

How to convert your music files with Windows’ new scripting environment

Tim Anderson, Personal Computer World 02 Jun 2008
ADVERTISEMENT

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

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
United Kingdom | Advent Computer Training
Are you stuck in a dead end job? Do you want to take control of your salary, life and career? Advent IT and computer training offers advanced, professional training and helps you find the right ... more >
United Kingdom | Hackney Homes
Hackney Homes Information Services Manager £46,737 - £53,196 p.a. (pay award pending) You'd be hard put to find another such opportunity to join a young and vibrant organisation in such an influential role. We are ... more >
London, United Kingdom | BP
Business Analyst - £ Competitive - London About BP Our business is the exploration, production, refining, trading and distribution of energy. This is what we do, and we do it on a truly global scale. ... more >
Hertfordshire, United Kingdom | Tesco.com
Senior Business Analyst - Hertfordshire Who's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at just under ... more >
More job opportunities