Cmdlets are the basic commands available within PowerShell. As such, they encapsulate code that performs useful work. This code is normally written by Microsoft. Cmdlets are normally written in a higher-level .NET Framework language such as VB.NET or C#. For example, the PowerShell documentation provides this C# example of a cmdlet that generates a random number:
using System; using System.Management.Automation; // GetRandom.cs /// <summary> /// an implementation of a random number generator /// </summary> [Cmdlet("get", "random")] public class GetRandomCommand : Cmdlet { protected override void EndProcessing() { Random r = new Random(); WriteObject(r.Next()); } }
This code would then be compiled using Csc.exe, which is the C# compiler provided with the .NET Framework Software Development Kit (SDK). Once compiled, it would be called from within PowerShell as follows:
PS> (new-object random).next()
As explained in Chapter 1, snapins are more or less collections of cmdlets. You can see which snapins, if any, are available but not active by running Get-PSSnapin and using the -registered parameter; in a base installation of PowerShell. Nothing will be shown because only the core PowerShell snapins exist that were all loaded into the shell by default. However, if you've installed third-party snapins, they'll be displayed. You can subsequently load them into the shell using Add-PSSnapIn:
Add-pssnapin MySnapInName
Creating new snapins is fairly complex and typically requires a high-level .NET language such as VB.NET or C#. It's beyond the scope of this book to cover cmdlet or snapin creation. However, these examples are provided so you can see a bit of what goes into them and how they're used to encapsulate more complex, high-level code for use within PowerShell.