Adding New Incrementing Number Column in a Grid View Window

by Oct 11, 2018

Maybe you’d like to add a column with incrementing indices to your objects. Try this:

$startcount = 0
Get-Service |
  Select-Object -Property @{N='ID#'={$script:startcount++$startcount}}, * |

When you run this chunk of code, you get a list of services in a grid view window, and the first column “ID#” is added with incrementing ID numbers.

The technique can be used to add arbitrary columns. Simply use a hash table with key N[ame] for the column name, and key E[xpression] with the script block that generates the column content.

Twitter This Tip! ReTweet this Tip!