Search

Thursday, 04 January 2007

Powershell - a new command line

Buried amongst the avalanche of software being released by Microsoft at the moment is a piece of software called Powershell. Its a replacement for the venerable DOS command line, providing all of its abilities and much more besides, its actually more akin to the power available from a UNIX/Linux command console than DOS but then I think that's where the inspiration comes from.

I've been playing with it now for a few weeks, well months actually if you include the betas that I've dabbled with and I have to say I like it; in fact I prefer it to DOS command line which I never really got along with. What impresses me most is that you can do very simple things with it, like changing a directory (it even provides the 'cd' command) or listing the content of a directory (again 'ls' is also present). But you can also do far more complicated things which before would probably have required someone to write a programme (certainly as a programmer that's what I would have done).

A feature that I've certainly found very useful, once I'd reset my security policy anyway, is its ability to run a profile script as the environment starts (a little bit like the old AUTOEXEC.BAT but just on that session of of Powershell). I've made use of this feature already to create a named 'function' (a named section of script within the larger script) for a common task that I've found myself performing a lot lately, and also some aliases to some other scripts.

An alias is effectively shorthand for some more longwinded code, while a function lets you string more than one command together (contained with curly braces) you can only alias one command. Where that comes in really useful is working around the annoying security feature that means that to run a script (a file with a 'ps1' extension) you must type the full path out (really annoying if its in your 'My Documents' folder), but using an alias this can become one short word. By putting the alias creation in the startup script you save having to type the full path each time.

Powershell has many other powerful features, its based on what is effectively the new Windows API (.Net) which means that if you are familiar with .Net you've got the whole of that framework at your disposal since you can create .Net objects and use them in your scripts as well as COM objects, very useful if you want to do some shell programming. That capability alone means that a Powershell script is probably a good alternative to a console application and a far better alternative to a batch script. I know my days of writing batch scripts are over!

The downside is that because there is so much more power on tap its a lot harder to learn, indeed Microsoft are aiming it at System Admins rather than normal users. But I think its worth the effort and I urge anyone interested to give it a go.

Comments
To leave a comment please login
Register