powershell - How to add a column of incrementing values to cmdlet output? -
suppose call get-service
, want assign new column id
cmdlet output prints incrementing integers that:
id status name displayname -- ------ ---- ----------- 0 running adobearmservice adobe acrobat update service 1 stopped aelookupsvc application experience 2 stopped alg application layer gateway service
i'm trying use select-object
right add column, don't quite understand how iterate variable in sort of expression. here's i've got:
get-service | select-object @{ name = "id" ; expression= { } }, status, name, displayname | format-table -autosize
is there way iterate integers within expression= { }
, or going problem wrong way?
you can way, though need maintain counter variable outside of main expression.
$counter = 0 get-service | select-object @{ name = "id" ; expression= {$global:counter; $global:counter++} }, status, name, displayname | format-table -autosize
another option, perhaps cleaner
get-service ` |% {$counter = -1} {$counter++; $_ | add-member -name id -value $counter -membertype noteproperty -passthru} ` | format-table id
Comments
Post a Comment