How to write progress bars with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
did you know that powershell has a built-in commandlet for writing progress bars well it does it's called write progress and it's quite awesome for every script that you write that is supposed to be run by your fellow humans you should include write progress just because staring at a mostly blank console with just the cursor blinking and really no way of telling you whether the script is doing anything at all or not well that's just irritating but right progress is here to save the day it can be used really simply or you can do it all advanced and cool so uh let's take it step by step shall we by the way this is the kind of things that i often do on my twitch streams so consider following me there and i catching these kinds of things live alright now that's done let's um take a look at our demos and this is our main setup which you'll use a lot of times during the demos and this is basically a simple script that does nothing it sleeps it sleeps it gets some processes it slips it sleeps it gets some services and then it sleeps some more and this is without any right progress let's see how this performs as you can see um my script started i have nothing more than a pretty blank console with a cursor blinking and really no way of knowing whether or not the script is stuck at some point uh if it's doing something important important or yeah basically anything i have the blinking cursor oh and now it's done and so what happens if we add in some right progress lines to show just some a little bit of progress bar so the way write progress works is that you have your activity which is your main what's what are you doing and then you have your status which is uh what are you currently working on for example and then finally you have a percent complete which is uh well basically the progress the percent completes part is what will steadily increase the progress bar as you can see here i have 10 20 40 50 60 80 and 90. this will just make the progress bar jump from 10 to 20 and so on and so on and um at the end here you can also see that i have added a completed part uh which is basically just to make the progress bar go away so that it doesn't stick this is a nice thing to keep in mind and yeah let's see how this performs as you can see when i'm running my script i have a nice little progress bar you can see that my activity is running your script and my status is sleeping for a bit and you can also see my progress bar is coming along steadily and it will not be step by step it'll be jumping from 10 to 20 and so on based on the numbers that i have put in my script so that's the basic usage of write progress uh it's a lot better than nothing but you can also add in some child processes and watch progresses if you want so say for instance you have a long script that has some sections in it and you can then have a main progress bar for your entire script and then you can have a sub progress bar for your each of your sections in the script and the way that you do that is i have added in a id for my main progress bar here and i have added another write progress line down here and i have given it an id of 2 and i have specified that the parent id should be 1 which will then refer to our write progress with the id of one and this is basically everything that i've done here um i have also added in a line that states that my oh sorry that it should be completed at some point and this is because this sub um progress bar will be for my get process part and then i want it to be gone and then for my get service part i will have it magically reappear down here so let's see how this performs you can see our the same running your script sleeping for a bit part once it reaches our sub write progress it will put it under here and you can see that has it all its own progress bar which you can customize to your liking uh the same way that you do for your main progress bar you just input the percent complete and once this is all done it should just clear up all the progress bars like so so that's uh a bit more advanced usage of uh right progress but let's do some cool stuff let's put all this into some for each loops and automate not only the calculation of the percent complete but also show a bit more relevant information to our user so the same kind of script but once we reach our get process here you see that i have added a dollar progress equals one this is a variable that we will use to calculate our progress and thus our progress bar and then i have a for each loop here which is doing a for each process in processes which we get on line 10 here and um as you can see our right progress is uh built up kind of the same way i have my activity which is getting all processes uh but in my status i have got and then my variable name which is actually referring to the name of each process in that is found by get process and then at the end here we add our percent complete do some math in order to calculate how far along we have come we use our dollar progress and then we divided by our processes dot count which is uh the total number of processes found by get process and then we multiply that by a hundred because that's how you calculate percentage and then below here i have just added a start sleep just to show you that something is happening because without it it would just loop through all of this too fast the last thing we do is increment our progress with uh one and we do that by just specifying dollar progress plus plus and that will take the number uh the progress has and increment it with the one so this for each loop will loop through each and every process found by get process and not only show some useful information to our end user about each process name it will also uh create a progress bar that will progress quite nicely and smoothly not jumping from 10 to 20 and so on and so on furthermore i've also done the same thing down here with our service part so it will do a same kind of for each loop for our services for each and every service found by get service it will show some information about service name and have a progress bar that will progress quite nicely and that's basically it so let's see how it performs at the start we have our usual 5 or 10 seconds of sleeping before it will reach our get process for each loop and here we can see that it will output the name of each of every process that is found on my computer some of the processes will show up multiple times so that is where you will see one process name just being stuck while the progress bar is moving along but as you can see the progress bar is moving quite smoothly along and then we move along and sleep for a bit more before ultimately reaching our get service loop and it will be just the same as for our process loop it will tell you the name of the service and the progress bar will progress quite nicely and smoothly and by the time that the loop is done we have a couple of more start sleeps and then the script is finished and this is the way that you have a really cool write progress or a cool progress bar which is actually showing some useful information in this case only the the name of the service or process but this can be expanded upon quite a bit for example if you have a long script that is creating users for example you can have have it print out the username for each and every user so you know just how far along it is not only by the progress bar but also by the names so this is uh something you can use quite a lot uh and that you should use quite a lot actually uh at least for for uh scripts that is made to be run by your fellow human beings if you liked this video found it interesting educational whatever consider dropping a like and subscribing and also consider subscribing to my twitch channel if you want to catch me doing these kinds of things live and yeah see ya [Music] you
Info
Channel: PetterTech
Views: 6,422
Rating: undefined out of 5
Keywords: powershell, automation, powershell tutorial, learn powershell, windows powershell, powershell scripting, powershell basics, powershell for beginners, powershell training, powershell examples, powershell tutorials, powershell 7, powershell automation, learning powershell, powershell scripting for beginners, powershell training for beginners, powershell demo, scripting powershell, powershell script, how to use powershell, powershell core, robocopy, getting started with powershell
Id: 5uAA1pE4d7w
Channel Id: undefined
Length: 11min 22sec (682 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.