Home > Net >  Separator(Write-Host) not recognised (Powershell)
Separator(Write-Host) not recognised (Powershell)

Time:03-11

Separator(Write-Host) (here) for BLANK whitespaces is not recognized when using it to display DELAYED output.

How to display spaces within list elements?

CODE

Write-Host "Started"
$startnum  = 1
$endnum  =  10
$list = $startnum..$endnum
$count = 0 
Clear-Host
while($count -le $endnum){
    Write-Host $list[$count]  -NoNewline -Separator " " //<--not recognized
    Start-Sleep 2
    $count  
}
Clear-Host
Write-Host "Finished"

OUTPUT (Separator " " not recognized)

12345678910 

CodePudding user response:

The -Separator is honored when you write out an array (one write process, many items).

Write-Host (1..10) -Separator " "

When you do the opposite (multiple write processes, one item each), make a string that contains a space at the end.

foreach ($i in 1..10) {
    Write-Host "$i " -NoNewline
    Start-Sleep -Milliseconds 250
}
  • Related