Powershell

cmd.exe

cmd.exe

powershell

Powershell console is a text based commandline environment on Windows with an end user interface similar to the traditional windows command line (cmd.exe). A few major differences are the smart tab completion and the backend implementation.

Traditional command lines such as windows cmd.exe and linux bash just execute programs that one specifies (like ls, ping, ipconfig, dir etc) and display the output on the screen. These programs figure out the executable to execute based on a variable they store(such as $PATH). These variables store a list of directories in which to search for the desired executable. For example, if the user types “ping”, cmd.exe will search the local filesystem for an executable called “ping.exe”, execute ping.exe, and display the output to the console. Once ping.exe is done executing, control returns back to cmd.exe, which will wait for another command.

Powershell is different. Unlike traditional command lines which are essentially text brokers (they just get the text from other programs and display them for you),  Powershell has the notion of “objects”. The powershell commands are of the syntax Verb-Noun, for example one might see “Get-Mailbox”. Objects returned from these commands are actually .NET objects, and all the text that is displayed is the result of printing the data member fields of those objects. For example, If one wanted to print the Mailboxes an Exchange Server Hosts, one would type “Get-Mailbox”. What the user would see on the screen is the result of iterating through a list of objects and printing the requested data for each object. In this respect, one can think of Powershell as an “Object broker” rather than the older “Text broker” model.

Another major difference between Powershell and the older command lines is the way in which new commands are made available in the console’s runspace. In the older model, the user could just drop an executable into a directory monitored by the shell, type the name of the executable, and it will execute. With Powershell, all the commands available in the current runspace are commands that have been loaded from a pre-loaded .NET assembly. These assemblies come in the form of .NET assembly dlls. First the dlls have to be registered with powershell. During registration, Powershell modifies its list (which is stored in the registry) of known assemblies. Once the dll is registered, Powershell must load the assembly. In the loading stage, powershell actually reads the contents of the dll and adds the commands defined in the dll to its list of known commands. Once loaded, a user can type “Get-Ma”, hit the tab button, and have powershell autocomplete guess “Get-Mailbox” if the correct assemblies are loaded.

Another cool innovation from Microsoft is remote Powershell. It is very similar to ssh on Linux, but it runs on Windows. Remote Powershell was a very needed feature in the windows environment, and could not have come soon enough.

Powershell is available for older versions of windows such as XP, Vista and Server 2008, but comes built into newer versions of Windows such as Windows 7 and Server 2008 R2. Many of Microsoft’s new technologies, such as new versions of Exchange use powershell, so it really pays for System Admins to become familiar with this technology.

References:

http://www.amazon.com/Professional-Windows-PowerShell-Programming-Providers/dp/0470173939

By: Neil Sikka

Save

Posted On : 2011/05/08

Search

Recent Posts

Labels

Blog Archive

WordPress Lightbox