PowerTip: Display Progress Bar with PowerShell

PowerTip: Display Progress Bar with PowerShell

  • Comments 5
  • 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?