An introduction to PowerShell Cmdlets
Windows PowerShell commands or Cmdlets (pronounced
as command-lets) are the commands which can be executed through PowerShell (PS).
At first glance, they look quite similar to the traditional DOS commands but
they are quite different from the traditional concept of commands, in which
people usually think of compiled console-based executable applications.
Instead, these commands are built on top of the .NET Framework and are .NET
classes usable only within the context of Windows PowerShell.
In other words, Cmdlets are really .NET
classes compiled into Dynamic Link Libraries (DLLs) that are loaded by Windows
PowerShell. They use the same memory space as the PowerShell process, which is
one reason why they are more efficient than console applications. Don’t believe
me? Wait, I will give you a proof.
To ensure that command names are intuitive
and descriptive, all Cmdlets are given names in the verb–noun format, in which
the verb describes what the Cmdlet does and the noun describes what it acts on.
Here are some examples of Cmdlets:
· Get-Service
· Set-Date
· Remove-Item
·
Write-Host
You
can find well over 200 Cmdlets defined within Windows PowerShell out of the
box. Although the available Cmdlets give you plenty of flexibility in and of
themselves, you can install additional Cmdlets from Microsoft (and even from
other vendors) to provide more application-specific functionality. Microsoft
Exchange 2007, for example, comes with the Exchange Management Shell, which is
a set of Cmdlets built on top of Windows PowerShell to provide enhanced
Microsoft Exchange management capabilities.
You can find all the Cmdlets by running Get-Command
on PS prompt. So let’s try out the classic ‘Hello World’ on PS console. Launch
PS from All Programs -> Accessories -> Windows PowerShell (if you have
Win7/WS08 R2, otherwise you need to install it separately). Once PS screen
launched and prompt is ready, type
Write-Host
“Hello World”
And press enter. It will execute the Cmdlet
and show the output. You can try out few more cmdlets, such as Read-Host (yes! You
got it correct; it’s for taking input from user). One of the striking features
of PS is its excellent help content, which can be accessed through Cmdlet:
Get-Help. If you want to see help content for a specific Cmdlet, type
Get-Help [Cmdlet]
I promised you to give you a proof that
Cmdlets are based on .NET framework. Let’s try out Read-Host Cmdlet as below.
$c = Read-Host “What’s your Rashee?”
In PowerShell, you can define a variable by
putting $ sign in the beginning of its name. Type the above line on PS Console
and Hit Enter. It will prompt the user like below:
What’s Your Rashee?:
Type some word (or even your Zodiac) and
press enter. The value is stored in $c variable. If you type $c on prompt and
hit enter, you can verify it. Now type $c and a dot “.” without any space
($c.), and start pressing Tab key. You will see all the methods which a .NET
object supports such as ToString(). The Tab key is like the IntelliSense of
Visual Studio. Try out working with some methods.
In the next article, we will explore
another surprising feature of PS, Pipelines