Power Shell cmdlets and pipelining

PowerShell is based on reusable libraries containing functions known as cmdlets (pronounced command lets). Cmdlets have names which follow the convention of a common verb followed by a noun. For example, the built-in PowerShell libraries provide a cmdlet named Get-Process which returns a collection of objects representing the Windows processes running on the current machine.

PS D:\Users\Administrator> Get-Process

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    400      45   217700     223408   461   299.30   5172 Acrobat
     80      10     1696       5188    72     0.03   5520 acrotray
     66       9     1632       4712    68     0.03   1780 Adobelm_Cleanup.0001
     66       9     1636       4672    68     0.03   6100 Adobelm_Cleanup.0001
     54       7     1104       3060    28     0.00   5176 Adobelmsvc
     41       4     1012       2508    15     0.00   1692 AESTSr64
     48       6     2136       4972    54     0.73   1680 conhost
     31       4      928       2392    22     0.00   4468 conhost

Pipelining in an important concept to understand when executing cmdlets. The basic idea is that every cmdlets returns an object or a collection of objects. Pipelining allows you to take the results of one cmdlets and pass it to a second cmdlet. The second cmdlets can run and then pass its results to a third cmdlets and so on. You create a pipeline by typing a sequence of cmdlets separated by the | (pipe) character.

cmdlet1 | cmdlet2 | cmdlet3

Example

if (dir | where {$_.length –gt 10kb}) {
“There were files longer than 10kb”
}

Blogger Labels: Pipelining in PowerShell,PowerShell cmdlets

0 comments: