PowerTip: Display Progress Bar with PowerShell

PowerTip: Display Progress Bar with PowerShell

  • Comments 7
  • Likes

Summary: Learn how to display a progress bar by using Windows PowerShell.

Hey, Scripting Guy! Question How can I easily display a progress bar by using Windows PowerShell?

Hey, Scripting Guy! Answer Use the Write-Progress cmdlet:

for ($i = 1; $i -le 100; $i++) {Write-Progress -Activity 'counting' -Status "

$i percent" -PercentComplete $i ; sleep -Milliseconds 20}

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • nice & easy...

  • I wrote a TechNet Wiki article that covers using Write-Progress in long running scripts. You can see it here: social.technet.microsoft.com/.../18697.an-example-of-using-write-progress-in-a-long-running-sharepoint-powershell-script.aspx

  • how do you link this to what is running in the script?

  • I am having a hard time adding a progress element to my PS script. It checks the status of urls and sometime its takes a while so i want a progress bar.


    ## The URI list to test
    $URLListFile = "C:\URLList\URLList.txt"
    $URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
    $Result = @()


    Foreach($Uri in $URLList) {
    $time = try{
    $request = $null
    ## Request the URI, and measure how long the response took.
    $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri -TimeoutSec 4}
    $result1.TotalSeconds
    }
    catch
    {
    <# If the request generated an exception (i.e.: 500 server
    error or 404 not found), we can pull the status code from the
    Exception.Response property #>
    $request = $_.Exception.Response
    $time = -1
    }
    $result += [PSCustomObject] @{
    Time = Get-Date;
    Uri = $uri;
    StatusCode = [int] $request.StatusCode;
    StatusDescription = $request.StatusDescription;
    ResponseLength = $request.RawContentLength;
    TimeTaken = $time;
    }

    }
    #Prepare email body in HTML format
    if($result -ne $null)
    {
    $Outputreport = "

    Website Availability Report

    "
    if($Entry.uri -notlike "http*")
    {
    $Outputreport += " "

    }
    }

    else
    {
    $Outputreport += " "
    }
    $Outputreport += " "
    }
    $Outputreport += "
    URL StatusCode StatusDescription ResponseLength TimeTaken"
    Foreach($Entry in $Result)
    {
    if($Entry.StatusCode -ne "200")
    {
    $Outputreport += "
    $($Entry.uri) $($Entry.StatusCode) $($Entry.StatusDescription) $($Entry.ResponseLength) $($Entry.timetaken)
    "
    }

    $Outputreport | out-file C:\Scripts\URLCheck.htm
    Invoke-Expression C:\Scripts\URLCheck.htm

  • what about -completed parameter, and about multiple cycles?

  • This is my first Post. I am new to Powershell and today is my day 1st day of learning Powershell. I'm enjoying Powershell as a newbie. Thanks for the Blog. I'll be reading your Blog from the beginning of the time.

  • I am trying to apply the write-progress cmdlet to the SCCM TS progress UI. Is this possible? Normally, you can use ZTIUtility and a WSF file to accomplish this but my whole goal is to eliminate VB from our TS and replace it with PowerShell wherever possible.

    So, I have a script that runs and captures an image. The default capture step in a TS will show a progress bar but this script is more for a backup option. Basically a step in the TS is loading a script that runs the new-windowsimage cmdlet. If this is run directly inside of PS, a progress bar shows by default. How could I feed that progress bar (hidden during the TS) to SCCM's TS progress bar. Or is that even possible?