PowerShell progress bars don’t hurt anything, but they don’t always add value. Here’s how to hide them.
Progress Bars: What are they good for?
We are a species that craves to know the current state of things, and if we can’t be told that, at least we wan’t to know SOMETHING is happening. And because of this fundamental truth, we have spinning circles, hour glasses of old, and progress bars.
My favorite progress bars are the ones that aren’t actually progress bars at all because they don’t actually tell you far how you have progressed through a process. They are more like animated gifs of a bar filling to let you know that SOMETHING is happening, much like the spinning wheel.
PowerShell progress bars are at least true progress bars; the increase in percentage and the bar itself is tied back to underlying execution logic. So if you feel compelled to watch, at least you are seeing an actual progress status.
Hide a PowerShell progress bar with $ProgressPreference
If you aren’t really one to stare at your screen while your script is in action (if I am running something that will take a while, I use that as an excellent opportunity to get coffee; I’ll read the log file later :), or you’d rather not have a progress bar that overlays and obscures your terminal, there is a way to hide it using the Preference variable $ProgressPreference.
The default value for $ProgressPreference is Continue, which displays the progress bar for cmdlets that utilize it (including Write-Progress):
PS C:\Windows\system32> $ProgressPreference
Here are the alternative values:
- Stop – Doesn’t display a status bar, but it also displays and error and stops execution. (This doesn’t seem overly useful? I wonder what happens when -ErrorAction SilentlyContinue?)
- Inquire – Doesn’t display a status bar, but that is because it interactively prompts you if you want to continue with the next “operation” that triggers a change in the status bar. Again, not sure how useful this is, unless your progress bar has a bunch of milestones you want to manually proceed through. And then it still displays the progress bar in the background. You can click “Yes to All” to only be prompted the first time.
- SilentlyContinue – Executes the command without displaying a progress bar.
Temporarily change $ProgressPreference
We don’t have to decide to hide progress bars for ever and always. Here is how we can hide a progress bar for a given command, and then revert to the default preference:
$OriginalPref = $ProgressPreference # Default is 'Continue'
$ProgressPreference = "SilentlyContinue"
Install-WindowsFeature -Name RSAT-DHCP
$ProgressPreference = $OriginalPref
And here is what that would look like:
Making Progress Bars – the other side of the coin
I’m not a complete progress bar hater. I think there are some use cases where a progress bar is handy, and in my next post I will cover how to create your own.
- About Preference Variables | docs.microsoft.com