PowerShell Pipeline ( | ) :
PowerShell pipeline tells PowerShell that you want to take the output of one command and pass it as the input (or pipe it) to the next command. Here we are passing complete objects. Using the pipe character (|), you can quickly and easily select objects and then perform an action on them.
When you “pipe” objects, PowerShell tries to associate the piped objects with one of the parameters of the receiving cmdlet. In a simple language, piping is sending the objects in the output of one command to another command.
Command-1 | Command-2 | Command-3
Let’s look at an example:
PS C:\> Get-Process Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 44 5 708 3164 44 0.03 888 conhost 57 7 1808 7408 53 4.31 3156 conhost 179 11 1620 3800 43 1.09 328 csrss 163 15 1680 10096 46 1.52 396 csrss 347 32 14420 20024 621 1.47 1336 dfsrs 102 8 1412 3920 22 0.02 1700 dfssvc 193 13 3160 10256 48 0.09 2132 dllhost 10284 7411 89816 88140 141 0.34 1368 dns 206 19 23368 35332 121 0.42 784 dwm 927 52 18184 47460 398 1.58 2776 explorer 0 0 0 24 0 0 Idle 89 11 1440 4280 26 0.00 1396 ismserv 1641 156 54608 63672 1177 7.14 492 lsass 122 14 4092 9532 95 0.45 1616 ManagementAgentHost 1601 38 35884 43488 576 0.61 1292 Microsoft.ActiveDirectory.WebServices 151 12 1992 6648 40 0.03 2236 msdtc 363 24 60784 69856 608 7.17 3148 powershell 258 10 3312 7504 29 1.78 484 services 53 2 296 1160 4 0.13 216 smss 440 28 7196 15560 89 6.52 1260 spoolsv 542 33 6700 15168 616 0.38 76 svchost 344 15 3464 10052 46 0.20 644 svchost 327 16 2684 6612 24 0.55 676 svchost 363 33 10408 12276 56 0.45 712 svchost 462 20 10872 14112 59 3.05 856 svchost 1113 43 15752 29468 124 5.88 892 svchost 735 24 5760 11568 81 0.27 936 svchost 227 17 8180 10376 615 0.13 1112 svchost 711 0 112 344 3 4.30 4 System 156 11 1500 6032 79 0.03 2864 taskhostex 148 11 2376 9768 105 0.80 3064 TPAutoConnect 164 12 2864 8328 70 2.31 1572 TPAutoConnSvc 162 15 1900 7872 44 0.11 732 vds 127 11 4356 10640 66 0.06 1508 VGAuthService 67 6 988 4044 45 0.02 804 vmacthlp 323 23 8004 17888 87 0.69 1580 vmtoolsd 196 18 5084 14604 104 2.13 2364 vmtoolsd 79 8 712 3628 40 0.48 388 wininit 153 8 1224 9156 54 0.58 440 winlogon 310 16 8560 15384 69 5.34 2244 WmiPrvSE
The above Get-Process with a pipeline:
PS C:\> Get-Process | Sort-Object ProcessName Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 57 7 1840 7436 53 4.34 3156 conhost 44 5 708 3164 44 0.03 888 conhost 163 15 1680 10096 46 1.59 396 csrss 179 11 1620 3800 43 1.09 328 csrss 344 32 14412 20028 620 1.47 1336 dfsrs 102 8 1412 3920 22 0.02 1700 dfssvc 193 13 3160 10256 48 0.09 2132 dllhost 10287 7411 89816 88148 141 0.36 1368 dns 206 19 23368 35332 121 0.42 784 dwm 927 52 18184 47464 398 1.66 2776 explorer 0 0 0 24 0 0 Idle 89 11 1440 4280 26 0.00 1396 ismserv 1636 156 54564 63636 1177 7.17 492 lsass 122 14 4092 9532 95 0.45 1616 ManagementAgentHost 1611 38 35844 43472 575 0.61 1292 Microsoft.ActiveDirectory.WebServices 151 12 1992 6648 40 0.03 2236 msdtc 399 24 66824 76924 609 7.36 3148 powershell 254 10 3260 7480 29 1.78 484 services 53 2 296 1160 4 0.13 216 smss 440 28 7196 15560 89 6.61 1260 spoolsv 1108 43 15700 29440 124 5.91 892 svchost 457 20 10788 14092 59 3.06 856 svchost 228 17 8228 10392 615 0.13 1112 svchost 735 24 5760 11568 81 0.27 936 svchost 344 14 3412 10036 45 0.20 644 svchost 542 33 6700 15168 616 0.38 76 svchost 355 32 10304 12212 55 0.45 712 svchost 325 16 2676 6600 24 0.55 676 svchost 711 0 112 344 3 4.30 4 System 156 11 1500 6032 79 0.03 2864 taskhostex 148 11 2376 9768 105 0.80 3064 TPAutoConnect 164 12 2868 8332 70 2.36 1572 TPAutoConnSvc 162 15 1900 7872 44 0.11 732 vds 127 11 4356 10640 66 0.06 1508 VGAuthService 67 6 988 4044 45 0.02 804 vmacthlp 196 18 5104 14624 104 2.19 2364 vmtoolsd 323 23 7996 17880 87 0.69 1580 vmtoolsd 79 8 712 3628 40 0.48 388 wininit 153 8 1224 9156 54 0.58 440 winlogon 307 16 7972 14852 69 5.39 2244 WmiPrvSE PS C:\> Get-Process | Sort-Object CPU -Descending Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 433 24 65412 75544 609 7.41 3148 powershell 1634 156 54564 63632 1177 7.22 492 lsass 438 28 7144 15544 89 6.69 1260 spoolsv 1107 43 15140 29032 124 6.02 892 svchost 307 16 8176 15080 69 5.41 2244 WmiPrvSE 57 7 1840 7444 53 4.38 3156 conhost 711 0 112 344 3 4.31 4 System 457 20 10788 14092 59 3.09 856 svchost 164 12 2860 8324 70 2.36 1572 TPAutoConnSvc 196 18 5140 14632 104 2.33 2364 vmtoolsd 252 10 3260 7480 29 1.78 484 services 163 15 1680 10096 46 1.66 396 csrss 927 52 18184 47464 398 1.66 2776 explorer 340 32 14412 20020 620 1.47 1336 dfsrs 179 11 1620 3800 43 1.09 328 csrss 148 11 2376 9764 105 0.81 3064 TPAutoConnect 323 23 8000 17884 87 0.73 1580 vmtoolsd 1631 38 35884 43556 576 0.61 1292 Microsoft.ActiveDirectory.WebServices 153 8 1224 9156 54 0.58 440 winlogon 323 16 2668 6588 24 0.55 676 svchost 206 19 23368 35328 121 0.52 784 dwm 79 8 712 3628 40 0.48 388 wininit 353 32 10252 12192 54 0.45 712 svchost 122 14 4092 9532 95 0.45 1616 ManagementAgentHost 546 33 6796 15204 617 0.38 76 svchost 10287 7411 89816 88152 141 0.36 1368 dns 738 24 5776 11584 81 0.27 936 svchost 344 14 3412 10036 45 0.20 644 svchost 53 2 296 1160 4 0.13 216 smss 228 17 8176 10376 615 0.13 1112 svchost 162 15 1900 7872 44 0.11 732 vds 193 13 3160 10256 48 0.09 2132 dllhost 127 11 4356 10640 66 0.06 1508 VGAuthService 44 5 708 3164 44 0.03 888 conhost 151 12 1992 6648 40 0.03 2236 msdtc 156 11 1500 6032 79 0.03 2864 taskhostex 102 8 1412 3920 22 0.02 1700 dfssvc 67 6 988 4044 45 0.02 804 vmacthlp 89 11 1440 4280 26 0.00 1396 ismserv 0 0 0 24 0 0 Idle
Multiple pipelines:
PS C:\> Get-Process | Select-Object ProcessName, CPU | Sort-Object CPU -Descending ProcessName CPU ----------- --- powershell 7.609375 lsass 7.40625 spoolsv 6.859375 svchost 6.109375 WmiPrvSE 5.453125 conhost 4.5 System 4.359375 svchost 3.125 TPAutoConnSvc 2.453125 vmtoolsd 2.421875 services 1.78125 csrss 1.734375 explorer 1.6875 dfsrs 1.515625 csrss 1.09375 TPAutoConnect 0.859375 vmtoolsd 0.796875 Microsoft.ActiveDirectory.WebServices 0.609375 winlogon 0.578125 svchost 0.5625 dwm 0.515625 wininit 0.484375 svchost 0.453125 ManagementAgentHost 0.453125 dns 0.375 svchost 0.375 svchost 0.265625 svchost 0.203125 smss 0.125 svchost 0.125 vds 0.109375 dllhost 0.09375 VGAuthService 0.0625 conhost 0.03125 msdtc 0.03125 taskhostex 0.03125 dfssvc 0.015625 vmacthlp 0.015625 ismserv 0
Some more examples:
PS C:\> Get-Service spooler Status Name DisplayName ------ ---- ----------- Running spooler Print Spooler PS C:\> Get-Service spooler | Restart-Service
The above command will get the service name “spooler” and restart spooler service.