Some cmdlets and scripts use progress bars to indicate progress. As you learned in the previous tip, progress bars cause delays, so if you don’t care about progress indicators, you may want to hide progress bars. Here is how that can be accomplished.
The code below downloads a picture from the Internet. Invoke-WebRequest does the heavy lifting and displays a progress bar while downloading:
#requires -Version 3.0 $path = "$home\Pictures\psconf15.jpg" $url = 'http://www.powertheshell.com/wp-content/uploads/groupWPK2015.jpg' Invoke-WebRequest -Uri $url -OutFile $path Invoke-Item -Path $path
If you want to get rid of the progress bar, use the $ProgressPreference variable, and temporarily hide progress bars. Note how the code is put into braces and called via “&”. This way, all variable changes inside the braces are discarded once the code is done, and there is no need for you to reset $ProgressPreference to the old value.
#requires -Version 3.0 & { $ProgressPreference = 'SilentlyContinue' $path = "$home\Pictures\psconf15.jpg" $url = 'http://www.powertheshell.com/wp-content/uploads/groupWPK2015.jpg' Invoke-WebRequest -Uri $url -OutFile $path } Invoke-Item -Path $path