The Beauty of Progress Bar in PowerShell 7.2 Preview 3

I’ve long used the preview versions of PowerShell but not cared for the experimental features available in those until we got the PSAnsiProgress feature in the latest preview version, 7.2 Preview 3

Now, I cannot say that I’ve been super active writing PowerShell lately. I’m actually quite embarrassed about it, but back when I did, I tended to use ‘Write-Progress’ quite a lot to get a feel for the running script’s actual progress. Now one problem Write-Progress is that it is always rewriting the information passed too it, resulting in a lot less effective code. Well, in 7.2 Preview 3, this has been partially solved. And by partially, I mean that it will still slow your code down, but @steve_msft has done a superb job minimizing that to a minimal. He has added a configuration variable called $PSStyle, which allows you to modify which progress bar you’d like to have. Minimal or Classical

Back to basics, to install PowerShell 7.2 Preview 3, either go to Microsoft Docs and install it manually or if you running Windows you could make it easy for you and run the below little snippet in a PowerShell Administrator window.

Invoke-WebRequest -Uri '' `
    -OutFile '.\PowerShell-7.2.0-preview.3-win-x64.msi'; Start-Process 'msiexec.exe' -ArgumentList '/package PowerShell-7.2.0-preview.3-win-x64.msi /quiet ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1' -Wait; if (!$Error) {Write-Output 'Completed'} else {Write-Output "Error: $($Error[0].Exception.Message)"}

Once Powershell 7 is installed, you need to enable the features by running a command. I’ll add them below because I had a bit of a problem figuring out how to utilise the new features for whatever reason.

# Enable only experimental features needed for the new progress bar.
Enable-ExperimentalFeature PSAnsiProgress,PSAnsiRendering
# Or you can enable all experimental features
Get-ExperimentalFeature | Enable-ExperimentalFeature

Now look at the old progress bar below, it’s not customisable, quite big and all that green colour & text is always rewritten to the pipeline for every time the progress bar updates, that’s a lot of writing. To clearify, every single change to the bar is rewriting the entire bar, even if it’s just the last file number that is getting updated in above example.

The New progress bar is minimal, just as the configuration implies. You have the possibilities to change the For/Back-ground colour along with some font changes.

If you’de like to try this out you can save the following snippet to a .ps1 file and run it.

$Windows = Get-ChildItem C:\Windows | Select-Object -First 116
$Total = $Windows.Count

$i = 1
    Write-Progress -Activity "Itterating through files" -Status "File $i of $Total" -PercentComplete (($i / $Total) * 100)  
    sleep -Milliseconds 60

So as an afterword I’d want to say thanks for coming by and reading this far. Feel free to ask any questions if you have any, and I’ll be happy to answer them, maybe you’ve used exprimental features before, if so please tell me in the comment secion below.

One thought on “The Beauty of Progress Bar in PowerShell 7.2 Preview 3

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s