Hiding write-progress when non-interactive

by davidski at 2012-12-16 07:58:37

I have a script that takes a while to run (>30 minutes). When running interactively (usually via the ISE), I have a number of write-progress indicators to provide feedback as to the script’s progress. The challenge is when scheduling the script as a job (using the PoSH V3 scheduled job cmdlets), as the output captured and displayed via receive-job also contains the progress bars, which is hard to look through.

Is there a better solution to not display the write-progress indicators when run as a scheduled task? I thought about adding in checks for whether powershell.exe is being run with the -noninteractive flag and wrapping all the write-progress with logic tests, but that’s a lot of code and obfuscation for a simple need. Hoping there’s a more clever way of approaching this!

David
by MattG at 2012-12-16 17:07:31
Hi David,

Assuming you run your commands either from the ISE or via a scheduled job, the following would be a simple solution:
if ($psISE -eq $null)
{
$ProgressPreference = 'Continue'
}
else
{
$ProgressPreference = 'SilentlyContinue'
}
The $psISE variable will only be populated if you’re in the ISE. Otherwise, Write-Progress output will be ignored.
by davidski at 2012-12-17 13:00:33
Whoa. I never noticed that variable before. Good stuff. That should be perfect for this solution. Thanks!
by nohandle at 2012-12-18 03:22:09
Another way is to read the value from$host.name