shell

PowerShell 3.0 Replaces $_

Q: I understand that in PowerShell 3.0 I no longer need to use $_ to look at attributes of piped objects. How can I view this information?

A: One of PowerShell's most powerful features is the fact that data returned from commands is kept in its native object format. This allows you to perform very powerful actions with a minimal set of commands. For example, you can use the following command to take all the network adapters from a virtual machine (VM) and set them to allow NIC teaming:

Get-VMNetworkAdapter <VM Name> | Set-VMNetworkAdapter -AllowTeaming On

The ability to pass and act on the entire object is very useful; however, sometimes you might want to look at a specific attribute of the passed object. To look at attributes of passed objects, $_ is used to represent each of the passed objects. For example:

Get-Process | where {$_.HandleCount -gt 900}

This command obtains a list of all processes (Get-Process) and sends the process objects to the next command (via the pipe), which looks at the HandleCount attribute of each process object to determine whether it's greater than 900 and then display it. For example:

PS C:\Users\john_000> Get-Process | where {$_.HandleCount -gt 900}

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                     
-------  ------    -----      ----- -----   ------     -- -----------                                                     
    940      43    14516      20420   151            6716 CcmExec                                                         
   2429     137   172264     134860  1105   317.41   3360 explorer                                                        
   1383      45    56512      35704   483    36.67   6388 GROOVE                                                          
   1528      25    11436      14228    66             720 lsass                                                           
   4966      94   212724     222680   921   275.41   1312 lync                                                            
   1301      49    44316      89204   437    19.67   2708 ONENOTE                                                         
   8127     158   227624     269428  1001 3,907.70   2496 OUTLOOK                                                         
   1381     240   363160     438572   883    24.58   6200 POWERPNT                                                        
   1330      62   250768     243924   967     2.28  10232 RDCMan                                                          
   1067      85   102900     101204   710            5232 SearchIndexer                                                   
    920      31    19956      20776   131             760 svchost                                                         
   1016      37    27136      26412   120             840 svchost                                                         
   3240      63    67340      65748   248            1028 svchost                                                         
    908      45    20004      24096   155            1088 svchost                                                         
    913      52    18284      14444  1295            1552 svchost                                                         
   1292       0   211384       1928   220               4 System                                                          
  43952      13     2600       3460    55            2892 WACService      

Notice that each object is essentially passed to the next command in the chain, where it's evaluated. The $_ lets you inspect specific attributes and even test them. People found the $_ a little confusing; in PowerShell 3.0 you can replace $_ with $psitem. For example:

Get-Process | where {$psitem.HandleCount -gt 900}

And it gets even better! In PowerShell 3.0 you drop the entire $psitem and the {}, which lets you write:

Get-Process | where HandleCount -gt 900

This is far simpler and better to use. But remember it's only possible in PowerShell 3.0 and later.

TAGS: Windows 8
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish