Write-Progress cmdlet not ending

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 4 months, 2 weeks ago.

  • Author
    Posts
  • #48186
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    Hi,

    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
    Write-Host
    $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
    Write-Host
    $i = 1
    foreach ($DG in $DGs){
            Write-Progress -activity “Retrieving members...” -status “Status: ” -PercentComplete (($i / $DGs.count)*100) | Out-Default
            $i++
            New-Variable $DG -Value (Get-DistributionGroupMember -Identity $DG.DisplayName)
            }
    Write-Progress "Done" "Done" -Completed | Out-Default
    Clear-Host
    

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
      Profile photo of Vandrey Trindade
      Vandrey Trindade
      Participant

      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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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.