So I wanted to get a simple "this is where we're up to" numbering to show in the host when running a powershell script. I created the below to mess around with this:
$musicList = Get-ChildItem "C:\Users\rlyons\Music" -Recurse -File | Select-Object FullName
$outfile = @{}
$num = 1
foreach ($sound in $musicList) {
$file = $sound.FullName.ToString()
$md5 = Get-FileHash $file -Algorithm MD5
$outFile.$file += $md5.Hash
Write-Output "Processed file $num"
$num ++
}
This works great! Except it gives me:
Processed file 1
Processed file 2
Processed file 3
Processed file 4
Processed file 5
etc etc. What I wanted was for the screen to clear every time it processed a file, so all I wanted to see was the number of the file processed change at the end of the line written to the host.
I tried adding in the good old Clear-Host
after the $num ++
and also at the start of the foreach loop, but all I get is a flickering line, and sometimes something partially ledgible. Yes, this is down to how fast the files are being processed, but I was wondering if there is a trick to this? To keep the Processed file
bit continously on screen, but have the number increment upwards?
Even better would be if it didn't clear the screen, so you could see all previously written outputs but have this one line refresh? Is this possible?
Note: I'm trying this in VS Code, Powershell 5.1