Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Pure PowerShell Post–Going Through Setting a Basic PowerShell Progress Bar–For future reference when I’ll need to use/demo a progress bar …

$
0
0

#STEPS FOR USING A PROGRESS BAR

 

 

#STEP 1 - Get your main objects collection you're going to browse and for which you with to monitor progression - it can be a Get-Item, or an Import-CSV or a manual list for example - $Mylist = "Server1", "Server2", "Server3", "Server4"

$MyListOfObjectsToProcess = "Server1","Server2","Server3","Server4","Server5","Server6"

 

#STEP 2 - We need to know how much  items we will have to process (to calculate the % done later on)

$TotalItems = $MyListOfObjectsToProcess.count

 

 

#STEP 3 - We need to initialize a counter to know what % is done

$Counter = 0

Foreach ($Item in $MyListOfObjectsToProcess) {

 

 

#STEP 4 - We increment the counter

    #  NOTE 1 : if we initialize the counter to $Counter = 1, then we can increment the counter at the end of the Foreach block - in this example we initialized the counter to $Counter = 0 then we increment it right away to go to "1" first ...

    #  NOTE 2 :Developpers like to start at "0" so usually you'll see counters increment at the end of ForEach-like loops - I'm just not following the sheep here, just because I'm French :-p

    $Counter++

 

 

#STEP 5 - Here is the core : Write-Progress - it comes with 3 mandatory properties : ACTIVITY, STATUS and PERCENTCOMPLETE

    #  ACTIVITY is the title of the progress bar

    #  STATUS is to show on screen which element it is currently processing (Processing item #2 of #20000)

    #  PERCENTCOMPLETE is the progress bar itself and it has to be a percentage (hence the $($Counter / $TotalRecipients * 100) as we calculate the percentage on the fly...

    Write-Progress -Activity "Processing Items" -Status "Item $Counter of $TotalItems" -PercentComplete $($Counter / $TotalItems * 100)

    sleep 1

}

 

 

$ObjectCollection | ft -a

 

<# Fore more information visit:

 

Powershell progress bar basics :

https://technet.microsoft.com/en-us/library/ff730943.aspx

 

A little bit more with Write-Progress:

https://technet.microsoft.com/en-us/library/2008.03.powershell.aspx

 

#>

 


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>