Ever wanted to display format-table or select-object with index that you won’t have to calculate yourself? I did. Even though I don’t use ft very often. I played with it today a bit and came with rather simple solution:
PowerShell, using GeSHi 1.0.8.6
-
$__counter = New-Object PSObject -Property @{Id=-1;Index=-1}
-
$__indexFT = @{
-
Name=‘Index’
-
Expression={
-
if ($__counter.Id -ne $MyInvocation.HistoryId) {
-
$__counter.Index = 0
-
$__counter.Id = $MyInvocation.HistoryId
-
}
-
$__counter.Index
-
$__counter.Index++
-
}
-
Width = 6
-
Alignment = ‘Right’
-
}
-
$__indexSE = @{
-
Name=‘Index’
-
Expression={
-
if ($__counter.Id -ne $MyInvocation.HistoryId) {
-
$__counter.Index = 0
-
$__counter.Id = $MyInvocation.HistoryId
-
}
-
$__counter.Index;
-
$__counter.Index++
-
}
-
}
Separate hast tables are there only because it’s illegal to use hash with not supported properties for that purpose, and because it looks nice in format-table if column has fine width and alignment. So all you had to do once you have it in module/ profile is:
gwmi win32_service | ft $__index, name, displayname => for good looking talbe or gwmi win32_service | select $__index, name. Should work fine in either way.