How to add numbers to lines of output

I am working on a PowerShell script that would list the “Name” and “State” of all the websites located on a specific server. I use the command below which works as expected.

Invoke-Command -ComputerName “WebServer01” { Get-WebSite | Select-Object “State”, “Name” }

OUTPUT

state name


Started website01
Stopped website02
. . .
Started website11

What I would like to do is have a number assigned to each line with a website. This way I could select a certain website by number then use this to perform an action (e.g. start, stop, restart)

OUTPUT

 state          name
 ------         ------

1 Started website01
2 Stopped website02
. . .
11 Started website11

I have used the command below to get the number of lines of output, but I am not sure how to use it, or even if this is the right approach to get the number of output lines.

$Test = Invoke-Command -ComputerName “$cmpName” { Get-WebSite | Select-Object “State”, “Name” | Measure-Object }
$Test.Count

OUTPUT

11

Any help would be greatly appreciated.

Hi,

Please edit your post to format your code as code, using the </> button. It really helps us with readability and copying and pasting code for testing.

Your command returns an array, so you could use the index property as your line number if all the elements are unique (if they’re not unique, the index will be the first occurance in the array which will mess up your numbering).

$Test = Invoke-Command -ComputerName $cmpName {Get-WebSite | Select-Object State,Name
$Test | Select-Object @{l='Index';e={$Test.IndexOf($_)}},State,Name