Managing an office full of Windows®-based servers and workstations has been challenging ever since the early days of Windows networking. While administrators have been using a combination of GUI-based applications, batch files, scripts, and a host of other utilities to get their jobs done, the GUI-based applications have tended to be the de-facto standard on the Windows platform.

Unfortunately, the nature of the GUI itself presents an inherent difficulty. A GUI is great for doing one operation on one system, but slow and tedious if you need to perform that operation on 1000 systems. In addition, GUIs are effectively hardcoded for a particular scenario, and typically cannot be used to solve novel problems. In many cases, the command-line approach can be more flexible and scalable, but Windows tools for the command line have, until recently, lacked the functionality and scope of their graphical brethren.

Microsoft® Command Shell (MSH), codenamed "Monad," is the answer to those who are looking for a more flexible and practical scripting environment for Windows. The goal of Monad is to provide a shell environment similar to the Korn, Bourne, or other shells on UNIX and Linux, and a rich programming language like Perl or Ruby, combined with the functionality of the Microsoft .NET Framework. Monad can do much of the heavy lifting, pulling information from Windows Management Instrumentation (WMI), for example, to vastly simplify systems management.

The possibilities offered by Monad offer a large and growing subject to study, and I can only scratch the surface in this column. I’ll cover the basics of Monad as it exists today, including an overview of the system and an introduction to Cmdlets and the pipeline. I’ll also give you instructions on how to get your hands on Monad and install it on your system.

What is Monad?

The name Monad comes from a philosophy called Monadism, a form of pluralistic idealism created by the 18th-century philosopher G. W. Leibniz, who also co-invented calculus. The idea behind Monadism is that the world is an aggregation of simple substances, the smallest of which is called the monad. The Monad product team is focused on this concept of composition, which drives the syntax, naming conventions, and many other aspects of this product.

In the context of Windows, Monad is a new shell and scripting environment. It makes use of small commands, known as Cmdlets (pronounced command-lets), which can be combined in rich and powerful ways to create a first class administration environment.

Monad itself is an environment that can be hosted within other applications, such as the new command shell, msh.exe. Monad is implemented as a DLL that you can host within your own applications, as well. You can create new administration tools on top of Monad (MMC snap-ins, for example, may eventually be layered on top of Monad), allowing you to use a GUI application to perform an operation, then use the script generated by this action to repeat the operation across multiple systems.

Installing Monad

[Editor's Update - 5/22/2006:
Monad has been renamed Windows PowerShell. Click here for more information, and to download a copy of Windows PowerShell.
]

Monad is currently in beta testing. Microsoft has released Monad both to a technical beta program and to the wider public as a part of the WinFx™ SDK beta. To install the Monad beta, you must first download and install the WinFx SDK itself. You can download the full WinFx SDK at Microsoft WinFX Software Development Kit for Microsoft Pre-Release Windows Operating System Code-Named "Longhorn"(Out of date link removed). This is a 442MB ISO image of the full SDK.

The Monad download includes a great getting started document, which I strongly recommend you read and work through. The learning curve is greatly assisted by the fantastic support on the Monad beta newsgroup.

Installing Monad is straightforward, although it must be installed on top of the .NET Framework 2.0 Beta 2. Once you have the .NET Framework installed, you can run the Monad installation package, msh_setup-i386.msi (you’ll need a different version for 64-bit computers). This setup package checks that you have the right version of the .NET Framework and completes the installation without requiring a restart. The application folder is, by default, "%programfiles%\Microsoft Command Shell".

Shell Basics

Once you have Monad installed, you can start it by running msh.exe. At launch, the MSH shell looks very similar to the cmd.exe command prompt. You can edit the msh.exe properties, turn on QuickEdit, change fonts and layout, and adjust the screen foreground and background colors, just as you can with cmd.exe.

Inside the Monad window, you use MSH by typing commands. For example, to list the contents of a folder, simply type the familiar DIR command, as shown in Figure 1. As you can see, the basic paradigm is the same as cmd.exe (type commands and get output), and most of the familiar commands are still supported. Nevertheless, this is a very different shell.

Figure 1 Running DIR in Monad

Cmdlets

Cmdlets are at the heart of Monad—Monad’s monad, if you will. Cmdlets are small executables, written in C# or any other .NET-compliant language, that implement a standard interface and are derived from a base CMDLET class. With the version of Monad available when writing this, you can even write Cmdlets within the interface itself.

Each Cmdlet has a name based on a verb-noun syntax. Examples include Cmdlets such as Get-Help, Clear-Host, Export-Csv, and so on. Each of these Cmdlets can take parameters as well as input from the pipeline (which I will explain later in this column). Cmdlets should be task oriented as well as self-documenting.

Cmdlets can also be aliased. The GetChildItem Cmdlet, for example, is by default aliased to both the dir and ls commands. Thus, an admin most familiar with the UNIX environment can type ls, while the Windows admin can type dir. Both result in the same output. This should make transitioning to Monad easier for many users.

When you start msh.exe, the engine runs two profile scripts called profile.msh. The first is a systemwide profile found at %allusersprofile%\documents\msh. The second profile is user specific, and is found at %userprofile%\my documents\msh. You can add alias statements to your profile.msh to customize your shell experience. I’ve created a number of customizations for my own profile, which you can download from the TechNet Magazine Web site.

Variables

As with most programming languages, Monad supports the concept of a variable to which you can assign a value. Variables in Monad are named starting with a $ character. For example, $date and $files.Variables can optionally have a data type formally defined. To display the value of a variable, just type the variable name (including the $). You can also do normal variable manipulation as follows:

Variables can be of any .NET type, including scalars and arrays. An array can be of simple types (such as a set of numbers or strings) or an array of objects (each of which has properties, methods to call, and so on). You can also assign the output of a Cmdlet to a variable, and then use the variable to access the underlying object’s properties and methods. Figure 2 shows an example of assigning the DIR command output to an array of file objects, and then using properties of the objects in the array.

Computer systems support a variety of data stores, and each store has its own concepts and (often non-portable) tools to manage the store. In Windows, you have data stores such as the file system (containing files, folders, and volumes) and the registry (containing keys and values), to name a few.

Providers in Monad present data from different data stores in a consistent fashion to existing Cmdlets. There are seven providers included with the most recent Monad beta. These default providers include: Alias, Certificate, Environment, Filesystem, Function, Registry, and Variable.

Using the Filesystem provider, the Get-ChildItem Cmdlet for that provider would get you the children of the current node, meaning the files and folders in the current working directory. Using the Registry provider, the same Cmdlet would allow you to retrieve the child registry keys and values of the current node.

The Pipeline

UNIX scripting shells have traditionally used the concept of a pipeline, whereby you string commands together and make the output of one command the input of the next command. The pipeline can be extremely powerful, but in the past has suffered one major disadvantage: the pipeline usually only passes raw text between commands. This means each command has to parse text output into a form it can use. That may mean, for example, dropping the first two lines of the text, moving over 23 characters, hoping that blanks are spaces rather than tabs, grabbing the next six characters, and hoping they’re a number. This is often referred to as "prayer-based parsing."

Monad provides a pipeline, but instead of passing raw text, it passes managed objects. That means a Cmdlet can use .NET reflection to look inside the objects being passed and see that the length attribute is an integer, and the name attribute is a string.

As an example, consider the Format-Table Cmdlet. It receives input and formats that input as a table, as shown in Figure 3. You should note that this example shows the range operator working in reverse.

You can also use the Where-Object and Sort-Object Cmdlets to filter and sort the objects in the pipeline based on one or more properties of the objects.

The Get-Process Cmdlet returns a list of all the processes active on your system. To look at just certain processes (say, with more than 500 handles) sorted by handle count, issue the command shown in Figure 4.

Reflection, a feature of .NET, allows you to look inside an object to determine what it is, what its methods are, and so on. A simple way to use reflection in Monad is to use the Get-Member Cmdlet. This Cmdlet takes each object passed in the pipeline and uses reflection to retrieve the object’s properties and methods. You could use this when debugging a script to work out what is actually being passed in the pipeline. Reflection is also a great tool to help you learn Monad. Figure 5 shows some examples of using Get-Member. Notice that gm is a predefined alias for Get-Member.

With Monad, I find myself constantly saying "Oh, and then there’s this other really cool feature." I’ve barely scratched the surface of the possibilities it offers. I haven’t even started to discuss the WMI interface, managing COM objects, or any of the more advanced administration topics, but I encourage you to explore further on your own.

Monad is slated to be released as part of the next version of Microsoft Exchange Server, currently planned for release sometime in 2006. Additional avenues of distribution are also being considered. Monad currently runs on Windows XP as well as current builds of Windows Vista.

Prerelease info in this article is subject to change.

Thomas Lee is Chief Technologist at QA plc, the UK's largest independent IT training organization. Thomas is a Microsoft Regional Director, an MVP, and MCT. He lives in the English countryside with his wife Susan and daughter Rebecca.