Adding a status Bar to Functions and Scripts in PowerShell.

Write-Progress Anyone?

greetings tourist,

Welcome to PowerShell City! My name is Xajuan Smith.

My first post is about progress, The “Write-Progress” cmd-let  that is….

I have not seen a lot of people use the progress bar for anything, But I feel obligated to suggest using this for long running scripts and other advanced multi-faceted projects that need to report it’s progress.

So lets start some tests with a total number :

$total=2000

or

$total= gc file.txt ; $total.count

or

$objects= import-csv c:\some.csv

$total= $objects.count

#this will count the lines or objects if you prefer that

lets create a variable to start the count at 0 before we do a foreach loop. and add the rest of the information below:

$count=0
write-host “”   #writing a blank

write-host “Time Script Started : $(get-date)” -ForegroundColor Green
foreach ($Object in $Objects)

{

$count+=1
write-progress -Activity “Copying $object.source to $object.target ” -PercentComplete ($count/$total*100.00) -Status “$($total – $count) Remaining to be Copied”

}

This will help hopefully unravel the mystery of the Cmdlet write-progress!

return to the City when you have time see you next visit!

also,

if you are doing a loop in a loop you can write progress for the internal loop by marking using the “-parentID”, “-Completed” and “-Id” parameter

thanks for visiting the City.

Published by Xajuan Smith

Computer Information Specialist, with a desire to make the masses feel what I was born to channel...The emotion that strives us to do more, speak more boldly and with most sincere ideals that will make life easier and will not hinder the dreams we all have at any given time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: