Share via


Write-Progress display - How to get rid of it when complete?

Question

Tuesday, December 13, 2011 12:37 AM

This seems straight forward, but I haven't seen/found the answer...

My progress bars hang around too long.  I would like them to go away when complete, but they don't always.

Typically the Write-Progress is within a loop, and it works.  Then imediately after the loop I add "Write-Progress -Complete", but that doesn't seem to do it.

What am I missing?

All replies (19)

Friday, October 18, 2013 2:09 PM ✅Answered | 5 votes

After your code where you use the write-progress use:
Write-Progress -Activity "<same as your progress bar>" -Status "Ready" -Completed


Tuesday, December 13, 2011 6:00 AM

Please post a sample of one that doesn't work as expected and outline what it is you are hoping it will do.  


Tuesday, December 13, 2011 3:44 PM

The loop and Write-Progress work fine.  The issue is that the completed status bar remains on the screen after completion.

 

#Some code...

While( -NOT $Script:RecordSetG.EoF )
{ #...Some code
 
 $intCounterL ++
 If( $intRecordCountL -gt 0 )
 { $dblPercentageL  = 100*( $intCounterL / $intRecordCountL )
  $intPercentageL  = [int]$dblPercentageL
  $intSecondsLeftL = ( $intRecordCountL - $intCounterL ) / 24
  Write-Progress           `
    -Activity         "Building mailbox objects..." `
    -PercentComplete  $intPercentageL    `
    -SecondsRemaining $intSecondsLeftL    `
    -CurrentOperation "$intPercentageL% complete" `
    -Status           "Please wait."
 }#End If
}#WEnd
Write-Progress -Completed

#...Some code


Tuesday, December 13, 2011 3:56 PM

I can't replicate your error.  When I run a progress bar in a loop, it disappears from the screen once the loop ends:

for ($i = 1; $i -le 100; $i++ ) 
      {write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;Start-Sleep -Milliseconds 10}
    

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Tuesday, December 13, 2011 3:58 PM

My initial thought is that you cll Write-Progress twice.  The first time has a different scope from the second.  So, I suspect the second instance is the issue.  Try commenting out the second one and set if the same issue persists.


Wednesday, December 14, 2011 9:39 PM

Not sure, but I don't think so...

There is only one instance of the write-progress cmdlet.  It executes many times within the loop.

Another question comes to mind... what is the -Completed argument for?  Should write-progress be called with that argument to close the display?  Or, when is that argument appropriate?


Thursday, December 15, 2011 1:10 AM

Not sure, but I don't think so...

There is only one instance of the write-progress cmdlet.  It executes many times within the loop.

Another question comes to mind... what is the -Completed argument for?  Should write-progress be called with that argument to close the display?  Or, when is that argument appropriate?

I see Write-Progress twice:

While( -NOT $Script:RecordSetG.EoF )
{ #...Some code
 
 $intCounterL ++
 If( $intRecordCountL -gt 0 )
 { $dblPercentageL  = 100*( $intCounterL / $intRecordCountL )
  $intPercentageL  = [int]$dblPercentageL
  $intSecondsLeftL = ( $intRecordCountL - $intCounterL ) / 24
  <em><strong>Write-Progress</strong></em>           `
    -Activity         "Building mailbox objects..." `
    -PercentComplete  $intPercentageL    `
    -SecondsRemaining $intSecondsLeftL    `
    -CurrentOperation "$intPercentageL% complete" `
    -Status           "Please wait."
 }#End If
}#WEnd
<em><strong>Write-Progress -Completed</strong></em>

I was suggesting eliminating the second one.


Thursday, December 15, 2011 3:44 AM

Experimenting with Write-Progress, it's a strange beast.

Try this on the command line by itself:

Write-Progress -Completed -Activity "Test"

You wil see that 'nothing' happens.  It has to be in a loop to stay on the screen.  That's what I find so strange about the OP's problem:  Maybe his loop in not ending?

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Thursday, December 15, 2011 4:01 AM

But, if that were the case, would it ever reach the second Write-Progress?  To the OP, try adding a Write-Host (or Write-Output in the loop and outside the loop prior to your second Write-Progress to see if you ever make it that far.


Thursday, December 15, 2011 4:28 AM

The definition of the Completed switch:

-Completed [<SwitchParameter>]
 Indicates whether the progress bar is visible. If this parameter is omitted, Write-Progress displays progress information.

Demo: Try this to see the effect of using -completed:

while ($true) {
    Write-Progress -PercentComplete 50  -Activity "Testing"
    Start-Sleep 1
    Write-Progress -Completed -Activity "Testing"
    Start-Sleep 1
    } # end while

 

 

 

 

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Thursday, December 15, 2011 6:40 AM

Not sure, but I don't think so...

There is only one instance of the write-progress cmdlet.  It executes many times within the loop.

Another question comes to mind... what is the -Completed argument for?  Should write-progress be called with that argument to close the display?  Or, when is that argument appropriate?

Hello CBS3,

Thanks for your posting.

As mentioned in document, "If this parameter is omitted, Write-Progress displays progress information."

You can also refer to the following links:

http://technet.microsoft.com/en-us/library/dd347663.aspx

Hope this helps, and any questions or confusion please let us know.

 

Regards,

Anders Wang


Thursday, December 15, 2011 9:52 PM

This is basically what the original code snippet was.  Originally there was only the single Write-Progress instance (within the loop).  Processing definitely completes within the loop and progresses beyond.

I added the second call to Write-Progress, with the -Completed switch, hoping that would "turn it off."

I also tried using the ID parameter in both instances of the command hoping to clarify the instance reference.


Monday, December 19, 2011 8:19 AM

This is basically what the original code snippet was.  Originally there was only the single Write-Progress instance (within the loop).  Processing definitely completes within the loop and progresses beyond.

I added the second call to Write-Progress, with the -Completed switch, hoping that would "turn it off."

I also tried using the ID parameter in both instances of the command hoping to clarify the instance reference.

Hi CBS3,

 

My initial thought is that the While statement is still running.

 

I did a lot of testing, but still can’t reproduce your error. Could you upload a screenshot about that error? Thus we can further discuss about this case.

 

Thanks.

Anders Wang

Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Thursday, April 19, 2012 2:29 PM

I can't replicate your error.  When I run a progress bar in a loop, it disappears from the screen once the loop ends:

for ($i = 1; $i -le 100; $i++ )       {write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;Start-Sleep -Milliseconds 10}    

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')

Your progress-bar just disappears because your SCRIPT is terminated. The OP put "#...Some code" after the loop and the progress bar still remain until the end of his script.

Add a "Start-Sleep -Milliseconds 100000000" after the loop and you will that the progress bar stay 100000000 Milliseconds after your loop...until the end of script.


Friday, March 21, 2014 8:27 PM | 1 vote

After your code where you use the write-progress use:
Write-Progress -Activity "<same as your progress bar>" -Status "Ready" -Completed

I was having this issue and this solved it for me.
Thank you.


Wednesday, June 25, 2014 4:57 AM

Yup - this worked for me!

Richard


Monday, January 11, 2016 6:54 PM

Take "Write-Progress" out of your code block under the "if" condition. "Write-Progress" should not be subjected to the results of "if" statements.... only loop iterations.

Write-Progress should go under the "$intCounterL++" above the "if" statement.


Monday, September 5, 2016 12:59 PM

After your code where you use the write-progress use:
Write-Progress -Activity "<same as your progress bar>" -Status "Ready" -Completed

Indeed.. if there is (a lot) more code after the write-progress action, the status bar remains visible for the remainder of the script.

Adding the code Write-Progress... -Completed, as quoted here, solved it for me.

Cheers!


Sunday, January 20, 2019 3:41 PM

Write-Progress-Completed close