Powershell

Table of Contents

Numbered Lists

Output numbered list

First prefill variable $array with sample string values

$array = 'abra','kadabra','alakazam'


Then run either option below to ouput the String into a numbered list to the host shell console.

# Option 1

$array | foreach -Begin {$i=0} -Process {
$i++
"{0} {1}" -f ($i),$array[$i]
}


# Option 2

for ($i=0; $i -lt $array.Length; $i++)
{"{0}. {1}" -f ($i+1),$array[$i]}


#Output


Interactive Console

get-service | where {$_.status -eq 'running'} | foreach -Begin {$i=0} -Process {
$i++
"{0}. {1}" -f $i,$_.Name
} -outvariable menu
$r = Read-Host "Select a service to restart by number"
Write-Host "Restarting $($menu[$r-1])" -ForegroundColor Green
Restart-Service $menu[$r-1].Split()[1] -PassThru –force


Or use Outgrid view

e.g.
gsv | outgrid-view

Visit next page to learn about - If Loop