PowerShell 7 Tutorials for Beginners #8 : ForEach (Loops)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the eighth video in our Powershell 7.2 for beginners tutorial Series in the last couple videos we've gone over some different conditional statements like the if else if else and switch statements now we're actually going to be taking a look at some loops and we're going to be starting off with the for each Loop so let me just start off with like just the basics of loops and why they are so useful and relevant in the programming world so there might be times where you have to repeat a certain action maybe based on some different inputs but the action that you're doing is most like mostly the same maybe just the content of the action might be a little different like example what we're going to be doing today is I'm going to be showing you how to create a bunch of folders um if you have a text file that has all the names of the folders you want to create and it'll go ahead and create them for you we're going to code it first um all manually so going through the folder iteratively without a loop and then we're going to see the for each loop we're going to see three different ways to create a for each Loop to accomplish the same task you're going to see they're they're all pretty easy they're all very similar uh there are some performance differences in some of them the last ones the last one that we're going to see is going to be the fastest so let's actually just go ahead and let's get started here so what I have is I have my folder with my script here I just named it for each dot PS1 and then what we have here is we have a text file containing some folder names I just named them some names of some departments that you would probably have in any uh company really like any IT company or probably like any company these days probably all have these uh these positions and then I have this folder called share and this is where I want to create all the folders so of course um the most manual way to do it would be to come in here and type these out now of course this could go very fast um depending on how good you are at typing but also just leaves it place for a bunch of Errors so it's not a super great uh a great method that's for sure so let's go ahead and let's take a look at doing it Brew code here so what we're first going to want to do as you probably guessed it from the last couple videos first we're going to want to grab all the folder names here so what we're going to say here is we're going to say a file path and we're going to go ahead and we are going to copy this file path now as you guys remember I showed you guys a little trick for this last time if you just come in your Visual Studio code and right click on that file just hit copy path and you can actually just go ahead and paste that in there that's the easiest way that I that I find anyways for getting a path of a file and then what we're going to do is we are going to get the content of that file so we can get all the different folder names uh so we're going to call that one folder names and let's go ahead and let's do a get content and we're going to do on the path file path all right so let's go ahead and let's run this here perfect so that actually works out great if we go ahead and look at the folder names now we are going to see that we get all the folder names here now as we've seen in the array video and we know that the get content pulls all this data back in an array so if we actually go folder names and we go square bracket zero uh we'll get the first item in the list which is marketing if we put one we will see that we get engineering and we have a total of uh five here so we're going to have to go up to number four because we have a zero based index so what we actually want to do now to create these folders is to actually go ahead and let's just say uh new Dash item and then we're going to say uh now here we're going to put in a path of where the folders are going to go so what I actually like to do in this case is I like to put a folder folders path and we are going to store them all to that share folder so let's right click on it let's copy the path that in there and let's just add a little slash here just this way it uh it's already we're already putting it in in the folder so that's going to be perfect actually we're going to remove that slash well we actually don't need it in this case I apologize for that um so in the new item here what we're going to do is we're going to put in the folders path for the path and then we're going to use the name parameter here and we are going to put in a folder names zero because we're going to be creating a folder with the name of marketing and then the last one is going to be item type and that is going to be a directory so we create a folder so let's just see what this does so if we just run all this it should create that first folder marketing so now if we actually go ahead and we paste this five times we run this as you can see it creates the file the folders for us if we open this up we get all the folders that created it pretty fast it took us about five lines of code now we have this issue though if we you know maybe add another one here so let's just go testing let's save that and let's run this again now of course it's not going to add that testing because we didn't add a Fifth Line to it and we're getting errors saying that the folders already exist um so let me just copy let me paste this once more let's add that in there and let's run it now we got the testing but we still have all those ugly errors so what you would probably want to do in this case um is like an if uh test Dash path and then you put in for the path here and you would put folders uh slash and then folder names uh zero but because this is actually you're using the square brackets as you're going to see these are taking in as a string so we're just going to have to wrap that in a variable here and then we can actually say if the test path is equal to false we're going to run that so now we would need to do this for all the items because now it's fine um and test path but we just need to wrap this as well here so now if we go here we're going to see we don't get any errors and if we do an else uh and we put in our else here just to see if it executes we can do right output folder exists so if we run this here we're going to see that we got the folder exists we don't get an error message or anything now we would have to do this for all five as you could tell now we're getting really really long it's going to take us forever and if we have a file of let's say a hundred or two hundred folders that we want to create this could take a very long time it would actually probably be faster at that point create them by hand but let's actually go ahead and let's take a look at the loops which is the main purpose of the video here so what we're going to want to do is let's get rid of all these actually let's put these at the top here so um actually we're just going to get rid of them we really don't need them we're going to use the for each so what we're going to do is to create our first version of the four each here we're just going to type in you probably guessed it the word for each and then open and closing parentheses and then in here we're going to say dollar sign dollar sign name in Boulder names and then after the parentheses you're going to do an open and close curly brackets and then in here what we're going to do is we're just going to put dollar sign name um what this is going to do this is just going to Output what the name dollar sign name variable is so let's go ahead and let's run this here as you can see we actually output all the items of our array so this will actually Loop through iteratively all the items in the array uh sequentially and assign them to the variable name for each one so it'll go through it'll be okay marketing is the first one assign marketing to name it does whatever is in the block here it goes to the next one it assigns engineering to name and it keeps going so what we can actually do is we can actually take this code we could paste this in here instead of referencing folder names zero all we need to do is reference name and then put name here and let's go ahead and let's just delete all these folders and what we're also going to do is we're just going to delete the testing so now we have our exact copy of what we had before so we created our our folders if we run it again we don't get any error messages we just get the message that the folder exists if we go ahead and we put testing in here we run it again it's going to create testing again not throw any errors though this is perfect like it didn't take us any longer we didn't have to add a line afterwards I can actually go ahead and let's just put uh testing to day three testing four testing five testing six and we could run this code again not making any changes and it will just add all the folders for us so as you can see the for Loops the for each Loops come very very much in handy but let's go take a look at a few other ways that we could write out this for each Loop uh so this is the first method the other method is using the for each object commandlet and our pipeline so what we can actually do here is we could take our folder names and then we're going to pipe that to A4 each object and then in here we're going to have a parameter called process and then in here we're going to do a open and close curly brackets and we're just going to put that in here and then all we need to do once again is just copy this code in here we're gonna have to make a slight modification so as we know from our pipeline video anytime that we pass in something through the pipe to reference that we need to use the dollar sign underscore so where we used to call it name we're just going to call it dollar sign underscore and just to show you guys maybe what that kind of looks like before we actually test it out here so if we just do the folder names pipe it to for each object and let's just do a right output dollar sign underscore if we actually run this here as you can see we get all the items in our array so let's actually just go ahead Let's uh Let's uh remove all those extra ones here we really don't need that anymore um so let's go ahead and let's reload the data in here and as you can see now we get our folder names with this dollar sign underscore so let's just go ahead cut and paste that in here so we have our saved if statement we have the exact same code just really with dollar sign underscore here and we can actually run this and it creates all of our folders now these two pretty much run at the same efficiency if you were to time them using the measure command they would perform pretty much equally uh now of course since we're only using five folders we wouldn't see any difference even if there was just because that size is too small I would always say like anything over a hundred is probably we're going to start seeing the differences and even that really depends on the processing power of your computer if you have tons of processing power it might not really be that much of a difference anyways um so let's go ahead and let's look at the last and fastest option for the for each year so the last one here is going to be dollar sign older name so once again we start off with our array and we actually use a DOT method so if we do a DOT and we start typing F we actually get a 4 each here and it's going to start with a open parenthesis we're just going to close that parentheses right away I'm just going to hit enter once and what we're going to do in here is we're going to do an open and closing curly brackets and also just do that now what we can do is we can also do once again the right output here now we're not using the pipe but we are using a DOT method which is partial kind of takes it the same way for getting the values so all we need to do to get our value is the dollar sign underscore in this case so if we actually just show this we get all of our names as you can tell so all we need to do is copy this if statement in here to this one and we will see that this creates all the folders as well now this is going to be your fastest option if you ever have a large for each or if you have a four each from an array I would definitely recommend using a DOT notation I know in my videos I don't really use the dot notation I usually use this for each just for readability for the videos and just makes it a little bit easier for people that haven't watched the beginner series to kind of understand what I'm doing but there might be some videos where I might use for each depending on the size of my array um and for production environments I would suggest using this for each it is a lot faster than all the other options again it's faster it really comes down to how your company codes and how you want to code if everyone is using everyone should be using the same standard so if everyone's using this for each I would just keep using this one if you guys are using this one for each object commandlet I would just keep using that one if you guys are already using the dot notation then I would keep using the dot notation as long as all the all the scripts are written in a very similar fashion it makes it easier to for people to kind of go in one script and go into another one and see how it's written and see what it does and understand it quite quickly now this isn't too vague so they would probably still be able to read it but it's just nice to have it all standardized but that is it for for each uh Loops I would definitely recommend any programming scripting uh coding worlds definitely use uh Loops we're going to be seeing methods a little bit later on in the intermediate course um which are another great tool to reuse code but if you're finding yourself copy pasting lines of code to just execute that line multiple times you're probably better off either using a for each Loop or a for Loop that we're going to see in the next video or methods you should never really be just writing the same lines of code over and over again there's probably a better way of doing it you shouldn't have to write your code iteratively it not efficient and it just makes a lot more typing for you and it opens you up to more errors just because you are typing more so your chances of typos are a little bit higher so I hope that helps you guys for the for each loops uh the next video we will be seeing are the for loops and the wild do loops so those are two different types of Loops that we're going to be seeing in the next video so be sure to stay tuned for that if you guys have any comments or questions please let me know Down Below in the comment section if it's something specific I'll try to answer you directly if it's something a little bit more generic I'll still answer you and I'll probably make a video this way it benefits a lot more people also be sure to hit that subscribe button hit that like button as well and also don't forget to hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 6,739
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, visual studio code powershell, programming, coding, scripting, powershell, powershell for beginners, how to start with powershell, scripts, powershell fundamentals, powershell 7.2, powershell 7 windows 11, windows 11, how to, beginners, powershell basics, powershell tutorial, powershell scripting, learn powershell, loops, loops in powershell, foreach, foreach loop in powershell, foreach powershell, loop, foreach loop powershell, powershell loops
Id: bOWucOWCjKA
Channel Id: undefined
Length: 19min 2sec (1142 seconds)
Published: Thu Oct 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.