As I continue to experiment with PowerShell v2 in Windows Server 2008 R2, I will share some of what I learn here on the blog. This time I am focusing on Cmdlets, Snap-ins and Modules. 

Cmdlets

Windows PowerShell introduced the notion of a “cmdlet” (you pronounce it “commandlet”). These are like tools or commands that are typically very simple and to the point (although most have properties or parameters). For instance, there is one to restart a computer (Restart-Computer), one to list the hotfixes installed on a computer (Get-Hotfix) and one to invoke a WMI method (Invoke-WmiMethod). You can get a list of cmdlets using a cmdlet called Get-Command. You can also learn more about a cmdlet by using a cmd-let called Get-Help.

Try these:

  • Get-Command
  • Get-Command -CommandType cmdlet
  • Get-Command | ?{$_.Commandtype -eq "Cmdlet"}
  • Get-Help Get-Command

Verbs and Nouns

You probably noticed that the name of the cmdlets are always divided into two parts, separated by a dash. The first part describes the type of action (called the “Verb”) and the second part describes the object where the action is performed (called the “Noun”). You will notice that the way verbs and nouns are used are quite consistent. You will see some verbs being frequently used, like “Get”, “Set”, “New” and “Remove”. Common nouns include “Item”, “Object”, “Service”, “EventLog”, “Computer” or “Job”. Not every combination of verb and noun is implemented though, you can use Get-Command and Get-Help to find more.

Try these:

  • Get-Command -CommandType cmdlet | Select Name, Verb, Noun
  • Get-Command | ?{$_.Noun -eq "Computer"}
  • Get-Command | ?{$_.Verb -eq "New"}

Parameters

Cmdlets also commonly include parameters. As with the regular command prompt in Windows and with most command-line tools, these will inform the cmdlet details about what to do. They are many times optional. There is usually a default parameter that can be provided right after the cmdlet name. Others must be explicitly declared by the parameter name starting with a dash. The Get-Help cmdlet, for instance, can be provided with a default parameter to indicate what cmdlet you need help on. You can also specify optional parameters like -examples or -detailed. Parameters vary widely between cmdlets. You can use Get-Help to learn more about the parameters for a specific cmdlet.

Try these:

  • Get-Help command
  • Get-Help Get-Command -examples
  • Get-Help about_parameters

Snap-ins

PowerShell can be extended in a number of ways and one of them is to add more cmdlets. The built-in cmdlets, for example, come from some PowerShell Snap-ins. A number of those snap-ins are loaded by default. By default, in Windows Server 2008 R2, the following snap-ins are loaded. You can get a list with Get-PSSnapin:

  • Microsoft.PowerShell.Diagnostics
  • Microsoft.WSMan.Management
  • Microsoft.PowerShell.Core
  • Microsoft.PowerShell.Utility
  • Microsoft.PowerShell.Host
  • Microsoft.PowerShell.Management
  • Microsoft.PowerShell.Security

The Get-Command and Get-Help cmdlets, for instance, come from Microsoft.PowerShell.Core Snap-in. You can load addition snap-in (with Add-PSSnapin). Snap-ins are basically .NET program compiled into DLL files and they can also include (in addition to cmdlets), providers and functions. The snap-in definition includes

Try these:

  • Get-Help snapin
  • Get-PSSnapin
  • Get-Command -CommandType cmdlet | Select ModuleName | Sort ModuleName –Unique
  • Get-Command -Module Microsoft.PowerShell.Core

Modules

With PowerShell v2, cmdlets can also be defined in another type of extension called Modules. By default, Windows Server 2008 R2 PowerShell will not import any modules, but there are a number of modules available for importing. These modules are described in files you can find under the c:\Windows\System32\WindowsPowerShell\v1.0\Modules\ folder, including files with a "psd1" extension that contain details about the module in plain text. Here is a list of the modules available in Windows Server 2008 R2:

  • ActiveDirectory
  • ADRMS
  • AppLocker
  • BestPractices
  • BitsTransfer
  • GroupPolicy
  • PSDiagnostics
  • ServerManager
  • TroubleshootingPack

The ServerManager module, for instance, provides cmdlets that can be use to manage Roles, Role Services and Features in Windows (they replace the deprecated ServerManagerCmd.exe tool).

Try these:

  • Get-Help Module
  • Get-Module -ListAvailable
  • Get-ChildItem C:\Windows\System32\WindowsPowerShell\v1.0\Modules -Recurse
  • Get-ChildItem C:\Windows\System32\WindowsPowerShell\v1.0\Modules -Recurse -Filter *.psd1 | Select Name, Length
  • Get-Content c:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServerManager\ServerManager.psd1
  • Import-Module ServerManager
  • Get-Module
  • Get-Command -Module ServerManager
  • Get-WindowsFeature
  • Remove-Module ServerManager

I hope this helped you understand a bit more about cmdlets in PowerShell. Try the sample commands (for Windows Server 2008 R2) and keep on learning...