Numbering Output (Part 1)

by Aug 8, 2019

If you’d like to add an incrementing number to your output, here is a simple way:

Get-Process |
  Select-Object -Property '#', ProcessName, CPU -First 10 |
  ForEach-Object -begin { $i = 0} -process {
    $i++
    $_.'#' = $i
    $_
  } -end {}

Select-Object adds a new property called “#”, and ForEach-Object adds an auto-incrementing number to it. The result looks similar to this:

 
 # ProcessName               CPU
 - -----------               ---
 1 AdobeCollabSync       65,5625
 2 AdobeCollabSync           0,5
 3 AGMService            
...
 

Twitter This Tip! ReTweet this Tip!