Write-Progress cmdlet not ending

This topic contains 4 replies, has 2 voices, and was last updated by  Vandrey Trindade 1 year, 10 months ago.

  • Author
  • #48186

    Vandrey Trindade


    I'm using the Write-Progress cmdlet to show a progress bar for each distribution group processed in a script.
    The problem is that it isn't closing after it reaches 100%.
    The only solution someone gave, searching over Bing, it was to use this in the end of the code:

    Write-Progress "Done" "Done" -Completed | Out-Default"
    Write-Host "Retrieving every distribution groups from Exchange Online..." -ForegroundColor Yellow | Out-Default
    $DGs = Invoke-Command -Session (Get-Pssession) -Scriptblock {Get-DistributionGroup | Select-Object -Property DisplayName}
    $DGs = $DGs | sort DisplayName
    Write-Host "Retrieving members of" $DGs.Count "distribution groups..." -ForegroundColor Yellow | Out-Default
    $i = 1
    foreach ($DG in $DGs){
            Write-Progress -activity “Retrieving members...” -status “Status: ” -PercentComplete (($i / $DGs.count)*100) | Out-Default
            New-Variable $DG -Value (Get-DistributionGroupMember -Identity $DG.DisplayName)
    Write-Progress "Done" "Done" -Completed | Out-Default

    We have 126 distribution lists on our organization.
    Any idea why my progress bar isn't ending?

    PS: The "Out-Default" code part is because the screen wasn't clearing in my powershell console after showing the progress bar.
    Read that tip on: https://kgk.gr/2011/10/16/powershell-clrscr/

  • #48189

    Dave Wyatt

    Try this:

    Write-Progress -Activity "Retrieving members..." -Completed

    Because you can have multiple progress bars going at a time, you should match up the -Activity names when you call it using the -Completed switch.

    • #48191

      Vandrey Trindade

      Hi again Dave Wyatt!

      So, the progress bar isn't supposed to disappear right after if completes 100%?
      The correct thing to do is always to use two lines?

      Write-Progress -activity “Retrieving members...” -status “Status: ” -PercentComplete (($i / $DGs.count)*100)
      Write-Progress -Activity "Retrieving members..." -Completed
  • #48193

    Dave Wyatt

    Correct, doesn't happen automatically. In general, I'll have the Write-Progress call with -PercentComplete inside a loop, and the one with -Completed as the next statement after the loop.

  • #48196

    Vandrey Trindade

    Nice! Thanks for one more lesson about PowerShell =]

    PS: There should be a way to mark questions as resolved in the forum...

You must be logged in to reply to this topic.