Windows Powershell for UNIX users

Introduction

People from the UNIX world learned to appreciate the powers of a shell like bash, tcsh, ksh or whatever. Under Windows you were traditionally restricted to either cmd.exe or using a GUI for your tasks. Very frustrating for UNIX guys like me. But in 2006 Microsoft introduced the Windows PowerShell. To be honest, I did not realize this shell exists until 2012.

First of all I have to say: I am not an expert on Windows PowerShell. I read a book on it and played around for a while. I am a UNIX guy, who loves his UNIX shell and I always have missed something comparable under Windows (besides installing cygwin maybe). Some day I had to install a Windows 2008 R2 server and came across PowerShell, which caught my attention. So i looked a bit deeper into what PowerShell is.

History of Powershell

PowerShell 1.0 was released in 2006, and can be installed on Windows XP, Vista and Windows Server 2003. PowerShell 1.0 has 131 cmdlets (built in commands). In 2009 PowerShell 2.0 was released and had 237 cmdlets and some additional features (transactions, jobs,...). Version 2.0 is preinstalled on Windows 7 and Windows Server 2008 R2. PowerShell is under heavy development and every new version brings a host of new features. A lot of Microsoft products (Active Directory, Exchange,...) and third party products (VMware, Quest,..) come with PowerShell snap-ins or modules to give PowerShell direct control over their products. As PowerShell seems to be a strategic product of Microsoft, the number of third party modules can be expected to rise.

Windows 8 will be shipped with PowerShell 3.0, including some interesting new features like support for workflows based on the Windows Workflow Foundation, running of scripts on a timely basis or based on events. PowerShell 3.0 can be installed on Windows 7 and Windows Server 2008 R2, but not on XP or Vista.

The following words refer to PowerShell 2.0, though most should be true for Versions 1.0 and 3.0 too.

Interactive Powershell

Basically interactive PowerShell usage works like in any UNIX shell. But the features are rather limited. You have a history of commands for every shell session. This history is not saved, when you leave the shell. Per default the size of the history is very limited, but can you can change this by setting the variable $MaximumHistoryCount. You can search the history backwards by editing the first letters and pressing F8. Tab-Completion is implemented too. There is no way to change the key bindings. But the good news is: PowerShell is implemented in a modular way. The console window that comes with PowerShell is called a PowerShell host. And the host uses the PowerShell infrastructure. It is possible to write your own host, e.g. a host that behaves like bash as far as line editing is concerned. There are some free and commercial alternatives to the default PowerShell host as well.

Built-in commands of PowerShell are called cmdlets. All cmdlets have the form <verb>-<noun>. To get a list of running processes you use the cmdlet Get-Process. There are a number of predefined aliases for cmdlets. So instead of Get-Process, you can type ps. You can also define your own aliases with New-Alias or Set-Alias. You don't have to use initial capital letters, so you can also write gEt-pRoCesS, if you want. If you need help for a command in UNIX, you use man <cmd>. In PowerShell it is Get-Help <cmd>. If you need more detailed help you can type Get-Help -full <cmd>. The following table shows a few examples of UNIX shell commands compared to analogous PowerShell cmdlets plus some predifined aliases in PowerShell. Of course UNIX shell and Powershell commands don't do exactly the same, but they have the same use cases.

UNIXCmdletAliases
lsGet-ChildItemls, dir, gci
cdSet-Locationcd, chdir, sl
pwdGet-Locationpwd, gl
mkdirNew-Itemmkdir1, md2
catGet-Contentcat, type, gc
whichGet-Commandgcm
manGet-Helphelp1
find .Get-ChildItem -recurse

1 Actually mkdir and help are not aliases but functions.
2 md is an alias to the function mkdir.

But it is not the single commands that we like so much about UNIX. The real power of the UNIX shell stems from combining these commands via pipes. Does the Powershell provide pipes? Yes it does, but the concept is very different. On the next page of this article I will show why.

Get in touch