none
String in PowerShell ProgressBar not Displaying RRS feed

  • Question

  • Hello:

    I have this code:

    $a = @("red", "green", "blue")
    
    $counter    = 1
    $totalItems = $a.Count
    
    foreach ($color in $a) {
    
    Write-Host $color "`n"
    Start-Sleep -s 3
    $counter += 1
    
    progressBar $counter, $color
    }
    
    function progressBar ($i, $s) {
      Write-Progress -Activity "My Progress Bar" -status "Doing stuff on $s" -percentComplete ($i / $totalItems * 100)
    }



    When the progressbar appears, nothing is getting added after "Doing stuff on".  It's blank.  The $s is not displayed in the status.  

    I looked at this example before posting, https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/29/add-a-progress-bar-to-your-powershell-script/, and I'm calling it the same way as the example given.

    How does one get the string to concatenate in the status field?  Thanks!


    Thursday, September 7, 2017 4:42 PM

Answers

  • I posted this same question on Stackoverflow.  That audience solved it -- and with some very good advice!!! Posting the link here in case anyone else run across this thread with the same questions:

    https://stackoverflow.com/questions/46007533/string-in-powershell-progressbar-not-displaying

    • Marked as answer by mgoblue0970 Tuesday, September 26, 2017 3:03 PM
    Tuesday, September 26, 2017 3:03 PM

All replies

  • Simplify:

    $a = @('red', 'green', 'blue')
    $counter = 1
    foreach ($color in $a) {
    	Write-Host $color '`n'
    	Start-Sleep -Seconds 3
    	Write-Progress -Activity 'My Progress Bar' -status 'Doing stuff on $s' -percentComplete ($counter / $a.Count * 100)
    	$counter++
    }
    


    \_(ツ)_/

    Thursday, September 7, 2017 5:13 PM
  • Still doesn't work.

    When running, the status displays, "Doing stuff on $s" instead of displaying the value of $s

    Monday, September 25, 2017 5:38 PM
  • Use double quotes instead of single quotes e.g. "Doing stuff on $s"

    Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.

    Monday, September 25, 2017 6:41 PM
  • Thanks @Vladdy!

    Same result though with 

     'Doing stuff on $s'

    as this:

     "Doing stuff on $s"

    Interestingly enough, the scripting guy article on this is with single quotes.

    Tuesday, September 26, 2017 3:01 PM
  • I posted this same question on Stackoverflow.  That audience solved it -- and with some very good advice!!! Posting the link here in case anyone else run across this thread with the same questions:

    https://stackoverflow.com/questions/46007533/string-in-powershell-progressbar-not-displaying

    • Marked as answer by mgoblue0970 Tuesday, September 26, 2017 3:03 PM
    Tuesday, September 26, 2017 3:03 PM
  • $a = @('red', 'green', 'blue')
    $counter = 1
    foreach ($color in $a) {
    	Write-Host $color '`n'
    	Start-Sleep -Seconds 3
    	Write-Progress -Activity 'My Progress Bar' -status "Doing stuff on $color" -percentComplete ($counter / $a.Count * 100)
    	$counter++
    }
    It does  not  appear  that  $S has  been  identified  if you  are  looking  for  what  is  being  completed in the  loop then  you  need  to  use  $Color ( that  gives  you  33% Red,  33% green,  33% blue.  The  change in the  variable and  changed the  single quotes to  double  quotes. 
    Friday, September 29, 2017 9:47 PM