Cmdlets: The Heart of PowerShell

At the heart of PowerShell is the cmdlet (pronounced "command-let"). It is the smallest bit of code you can run. A cmdlet is a block of code that you can run from the PowerShell prompt, or from a script, that does performs a function. Technically, a cmdlet is a .NET class that has been developed by either the PowerShell team or a third-party developer and incorporated into PowerShell.

To call a cmdlet, just type its name at the PowerShell prompt, as follows:

PSH [D:\foo]: Get-Process

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    112       5     1220       3640    32     0.06    436 alg
     42       2     1316       3424    29   199.19    576 ApntEx
     93       3     1740       5776    37   494.06   2456 Apoint
    267       7     4492      12140    64     1.61  47124 Connect
   1045       9     7220       7552    51   242.39   1680 csrss
     74       4     1284       4824    30     1.84   3864 ctfmon
    107       4     3624       7224    47     0.67   1016 DataServer
    142       6     4636       8472   179     1.09    372 EvtEng
... [output snipped]

In this example, the Get-Process cmdlet obtains the set of running processes on the computer and outputs a number of attributes. The attributes that are output by Get-Process (i.e., handle-count, non-paged kernel memory, paged kernel memory, working set, VM size, CPU time used, process-id, and process name), are output by default and are sorted based on process name.

Cmdlets can take parameters, as shown here:

PSH [D:\foo]:Get-Process notepad Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 48 3 ...

Get Windows Server 2008: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.