A First Look At Monad

One of the things that makes UNIX so powerful is that it consists of numerous utlity programs that only do simple things. You use those utilities by linking them using pipes. For example:

zef@zerver:~$ cat unsorted.txt
Jake
Pete
Zef
Adia
Anna
Monica
Ross
zef@zerver:~$ cat unsorted.txt | sort
Adia
Anna
Jake
Monica
Pete
Ross
Zef

All an utility has to do is interpret the input, do something with it and then send the manipulated data as text to the output.

This is where Monad builds on. Monad is the code name of the new Windows Command Shell (WSH). WSH is the replacement of Windows’ standard console (cmd). It adds substantial features that make it a whole lot easier to use and way more powerful than it was.

*Providers*
Let’s start off with the simple stuff, browsing your harddisk. Like with older versions of the console (and MS-DOS before that), there are basic commands like cd and dir. Additionally some more UNIX commands are added, such as ls, cat, pwd and probably more:

Simple commands in WSH

What’s different is that there’s now a more generic way of browsing like this. What you can do is create your own so-called providers. Providers implement a certain interface and can allow anything to act like a file-like system. By default you will use the FileSystem provider, but there are many more:

Providers in WSH

The registry for example:

Providers in WSH

With the package I received there was also a provider for XML documents but I couldn’t get that to work, sadly.

*Cmdlets*
Cmdlets (command-lets) are little managed programmes (written in a .NET language) that are like a UNIX utility. It does something with its input (if it has any) and produces output. The difference is that it doesn’t produce text, it produces a stream of .NET objects. You can pipe through this stream of objects to other Cmdlets which can then manipulate them. What’s nice about this is that there’s no need for your input and output to be pure text and also you don’t have to do any parsing, it all works automatically. For example, when I want to order the list of currently running processes in a descending fashion by the process names:

Pipes in WSH

No need to parse the output of get-process, it’s just a stream of objects that you can manipulate. A very simple Hello World cmdlet (that takes no input) would look like this:

using System.Management.Automation;
[Cmdlet( "Invoke", "HelloWorld" )]
public class InvokeHelloWorld : Cmdlet {
protected override void ProcessRecord() {
WriteObject("Hello World");
}
}

There are also Cmdlets that convert object streams from and to XML files or even to Word or Excell files.

*Scripting*

WSH also has scripting capabilities. These capabilities integrate very nicely into the .NET framework. Some examples:

MSH>$name = "Zef Hemel"
MSH>$name
Zef Hemel
MSH>$name.ToUpper()
ZEF HEMEL
MSH>$name[0..2]
Z
e
f
MSH> $a = @{one= 1; two= 2; three= 3}
MSH> $a
Key Value
--- -----
one 1
three 3
two 2

But that’s just simple stuff. You can do much more complicated things like conditional statements and loops aswell.

*Extensibility*
The whole system is extensible. You can create your own Cmdlets and your own Providers. Cmdlets can be written using the cmdlet scripting language or any .NET language such as C# or VB.NET.

*More information*
More information can be found in this “Channel9 video”:http://channel9.msdn.com/ShowPost.aspx?PostID=25506 (including information on how to get your beta copy of WSH) and “this .NET Show episode”:http://msdn.microsoft.com/theshow/Episode043/default.asp.