Write-Progress cmdlet not ending


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/

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.

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

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.

Nice! Thanks for one more lesson about PowerShell =]

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