PowerShell Fundamentals Differences between Format, Select, and ForEach

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mike and welcome back to the channel in today's video we're going to go over some fundamental topics here uh dealing with the difference between the commandlets that work with formatting selecting and using for each so I chose this topic because in the most recent classes that I've been teaching I've noticed that and I've also noticed this in the past that students have had a lot of trouble understanding the differences between these things uh so figured it would be a good topic for a video and maybe a good review for anybody who maybe is a little more experienced than Powershell as well to kind of know you know what's kind of happening between them okay so let me uh switch over to the lab environment here and let's go ahead and uh and get started so up on screen here I have basically a just simple Powershell console I'm running this on a 2022 domain controller uh so that's kind of where I'm setting at here so to start with here let's say we're running a commandlet so let's say I'll do something you know simple like get service maybe looking at uh I know a couple services that are there so the first thing to understand when we're looking at Powershell is what it does for output so when you run a commandlet and that commandlet produces some type of output that output is in the form of a.net object okay um so so that that.net object or objects plural in this case because it is producing more than one are placed into an area in Powershell called the pipeline and basically this pipeline is for holding these dot net objects temporarily as you are running some some code on your line here right so in this case I have a single command lid with with a parameter on it now the purpose of it uh this pipeline here is to kind of hold these these dot net objects in the situation where maybe you want to take the output of one commandlet and pipe that to be let's say pipe that to be the input or the the data that another commandlet is going to use so in this case here before I hit enter uh this is producing a.net object and that.net object is going to be used by this commandlet here diet like like so um so if I kind of look at get service now we could see that it is indeed uh running the bed service right so that's kind of one use of these.net objects that are held in powershell's pipeline to be able to take them and move them from the output of one commandlet to now be the input for another commandment right and I've got a video up but I think I did this one way back in the beginning of my channel um called which works with uh pipeline parameter binding so this is basically that the system the Powershell uses to determine if there is a.net object in the pipeline produced by one commandlet what parameter does that object get sent to on the next commandlet here so that is using a system called pipeline parameter binding now what if though you don't have a commandlet to send the data to so this produces an object a.net object it's passed over to this commandlet well what if you don't have that like this scenario up here um so if I run this you'll notice that we see screen output instead now what's happening with that is this commandlet produced the.net object or objects in this case put them in powershell's pipeline but there's nothing else left to do this this command line if you will is basically finished okay so what Powershell does is it sees that there is these dot net objects in the pipeline and there's nothing else to run there's no other code to run so it outputs them to the screen and so that's what caused this screen output to appear it's the fact that there were some.net objects left over in the pipeline after all of this code ran all of this I mean it's only one commandment but still all of this code ran okay so a lot of what we do in Powershell is around manipulating those.net objects and powershells Pipeline and that's where formatting selecting and for each come into play okay so so let's talk about like the first one here actually before we even talk about that let's talk about what just happened here let's say by it you know by just Powershell doing its default Behavior so when there's objects left in the pipeline and they have to be outputted to the screen uh Powershell will follow some rules for how it does that all right so basically the rules are kind of determined based on what type of object is in the pipeline so if I look here if I take this this commandlet that would produce that uh that output and I pipe it over to a commandlet called get member uh that would show me what was being placed into the pipeline so literally like what type of object was placed into the pipeline okay and this is the full you know namespace information and class information for this object uh straight out of.net framework so you could actually take this pop it into a search engine and you know Google it or whatever and you would get the Microsoft documentation for this type of object that would show the uh the the properties and methods basically the membership if you will that's available on this object so methods represent actions I can take against the object properties represent facts about the object and so these are kind of like straight out of the.net framework right um events are a programming thing so if you want some type of programming hook uh you could use that and then these other ones here Alias Alias properties and script methods these are things that are added onto that object while it's existing in Powershell so they don't per se exist in the.net framework documentation they're just kind of added on if you will right so that gives us an idea of what object we're working with now what's actually kind of a little confusing to start with though is when you're first doing your output uh you see you don't see all of that information right so these are the properties that I see but these are all that are actually there okay so that's kind of a big difference now what Powershell does by default here is it uses something called default formatting where if there's a certain type of object in the pipeline and that object has to be written to the screen Powershell in a lot of cases has like a almost like a recipe it needs to follow in order to Output that to the screen now a lot of the default command lists that come with Windows if you look in powershell's installation directory let's go to Windows system 32 Windows Powershell let's go find that uh V1 uh and notice this.net types dot format.ps1xml so that is a view definition file and Powershell and if I open it with just like Notepad um and I look here you can see the different views that have been defined for the different types of objects okay so like for instance here I was looking for a service controller that's what the commandlets I've been running have produced the get service commandlet uh so if I look here I can go find like for instance uh the view right for this type of object where if it needs to be outputted to the screen you could see it wants to make a table uh with column headers that are eight characters 18 characters and 38 characters wide and then within those columns we're going to write the status property the name property and the display name so that is the recipe for this output right here okay so it made a table it made 8 18 and 38 as the widths for those columns and then it had status name and display a name as the properties from the object that was in the pipeline okay so we could see there's name right there you know there was status right there and display name is right there okay so that's what Powershell does by default all right now a lot of commandlets uh per whatever.net objects they produced uh they can also have other uh views defined for them so if it's not just commandlets that come with uh with Windows if you're talking active directory or SQL or exchange or Azure or something like that uh views can also be defined in those individual modules themselves okay so just think of you as as kind of like a recipe for when an object has to be produced or displayed to the screen uh what is it going to do right so if we have an object you know how is it going to display so in this case these three properties were chosen as a table right now what if I want to change that up okay so so the first thing we could do to kind of change how maybe the data looks on the screen is to use formatting commands and the probably the two most popular ones would be um a format table and format list so if I run a format table uh with this commandlet here you might notice it doesn't look any different right because it was already doing a table to start with however with format table there's parameters I can use like property where if I want to make up my own table with my own properties you know something like that and just be aware whatever property you're right it has to be in the original.net object okay so you can't just make stuff up um so we could make our own right now the thing to remember though with the formatted commands is they modify the screen output so they're pretty much useless if the command lit uh that you're piping the data to does not produce any output to begin with okay so it has to be making screen output right so so this gets our table now you could keep this so it's just displaying to the screen or you could use a commandlet called out file uh if you want the information as it would display it on the screen written out to a text file so let's do out file I will give a file path uh see colon demo and we'll call it out one dot text uh because that's basically what it does so if I go and look at this let's go back to my C drive go to demo and out one you can see what's actually there right so out file uh is just basically a commandlet that's taking what would have shown up on the screen exactly the way it would have shown up on the screen and just writing it out to a text file right that's all it's doing uh now if you use though the formatting commands and then make some other type of output like if I do export CSV uh let's again give it a path uh see colon demo let's call this out2.csv uh one of the things you'll find is the output for it is going to look like a bunch of gibberish okay so if I just open this um uh let's maybe open it with notepad here uh you'll notice it looks like a pile of garbage if you will and what's actually happening here is when you use the format commands let me uh let me kind of pull this take the export out of here and show what's actually happening in the pipeline by piping whatever this is producing for output over to get member and you'll notice that within the get member uh the service controller that we were working with originally that this commandlet produced for the.net object you'll notice it's nowhere to be found here instead we have a bunch of formatting objects that have been put into the pipeline which isn't bad in and of itself but these formatting objects are only there for doing your screen output right like so uh if these are then you know written to something else like export CSV uh convert to HTML export CLI XML uh they're not going to produce this this data in whatever that that next output file would be okay so when we're looking at the formatting commands uh it's basically like you want to use them when you're just going to want to look at the data on the screen or you're going to want to do an out file okay that's basically about it now another good use though if maybe the formatting commands would be uh instead of format table we'll do format list right and that when you run it it takes what objects are in the pipeline and displays them in list format with their properties on the left and their values on the right okay and you can see here's the first object there's a space and then here's the second object right so with this uh you could see a little more detail about what's happening with your objects okay and format list if you also specify you can specify what properties you want if you wanted to do that okay so I can look for name status and start type and it displays it out as a list now another popular thing to do with format list is what if you want to see all of the properties you could just do a wild card there um and then you know notice you're seeing everything right so if I want to see everything as in every property and its value even if it's empty uh we could use this using format list okay now uh in case you're not familiar um I'm typing kind of all of these commandlets out with the full commandlet name okay so there's also aliases uh that we could use so for instance a format list can actually be Alias down to uh just f l right um and format table likewise would be ft and that can work okay so you can see that that ran right there and then lastly uh the property parameter is what we call positional so the property parameter is positional that's a lot of P's there uh and so basically you can just provide the value without having the actual property name there so if I get rid of this uh that can now be all you have to type in to see all of the properties up on the screen right so so we went from you know format list you know like this uh with everything specified out you know down to like that all right so that's kind of a look at at formatting it and what it's for now the next thing could be if I want to do selecting okay so so with this uh with selecting we're going to use a commandlet called select object now let me kind of preface this ahead of time what select object does at least in the context that I'm talking about it here is it allows me to specify what object what properties do I want to keep on the objects in the pipeline so a select object does is it manipulates the object that's in the pipeline okay so like for instance here if I pipe this over to get member which is an alias of GM uh we saw this earlier I can see what properties are there right so all of those properties are there and if I were to write this data out to a file let's maybe do the export CSV and then give it a path see colon demo let's do out three dot CSV uh we could see that it erode all of that information out right so we have our CSV format so there's our header row with all of the properties and then here's the first object with the actual data for each of those properties and the second object right there and notice it did everything it wrote out every property on each object into this file so what if I don't want everything okay that's where potentially select object could come into play beforehand so so again we saw what was in the pipeline so this was the service controller right this is the service controller with all of its properties and so if I take this over to select object I can specify using the property parameter uh what ones I want let's do status and maybe oops start type did I spell that right yeah okay so I go ahead and I run to that oops helps if I do a comma instead of a period uh there we go and so we could see uh what's happening there now if you're kind of you know paying close attention here you might realize like well wait a minute Mike uh this output looked the exact same as when you did format uh format table and if you look at it on the screen you're not wrong okay but here's the difference if I pipe this over to get member notice the original object is still there it's moved into a different name space it's now in the selected name space but it's still the original object if you will and notice like all of the methods that were specific for the service controller have been stripped off uh you've got a couple just generic methods that go with every.net object and then all of the properties are now missing except the ones that I told it to hold on to right so it's still keeping the original object that was in the pipeline so this produced the service controller and this is basically modifying that service controller object just leaving these three properties so now what's kind of cool about this is now I could export this in in CSV format and just literally keep exactly those properties so again there's my object there's my header row and then there's the two objects with the three properties I wanted okay so that's that allows me to kind of fine tune uh that particular uh that particular output right so so kind of work with that object okay so that's kind of using select object now uh you can also abbreviate uh there are some aliases here uh so for instance select object has an alias of just select all right so if I run that you could see it produces that and then lastly um we could get rid of property right because that is a positional parameter as well so these data values are fed indivisibly uh to the property parameter behind the scenes that's what a positional parameter is right um so that would work as well right and in fact if you wanted to use let's say you wanted to grab all of the uh all of the properties and you want to see it on the screen you could actually use select object to do that as well because I I had mentioned earlier right you could do format list you know property star and that would allow you to see all of the information well you could also do select object um and do property Star as well and that would mimic the behavior of format list okay where you could see all of the properties and you know again you could Alias this out as well uh use positional parameters there and then just do select and you kind of get the same result so so you could do select Star as one way of seeing all of the properties for the objects up on the screen and you could also do format list Star right so either one kind of works right now in some situations though um selecting uh may not be what we need to do right um so what I mean by that is when you look at let's say you know some form of uh of output in the pipeline everything in Powershell kind of is in the form of.net objects right whether we uh whether we like it or not and let's say for instance I go back to the original commandlet here get service pipe that over to get member notice that for all of these properties like you see status display name machine name uh start type okay they are Again Properties for this object here the service controller object but their data like literally the info that's inside each of those properties that is an object in its own right okay so so inside of an object you have properties and the data in those properties are objects themselves so if you look at like let's say service name that is a string object guy if you're not familiar string is basically a bunch of ASCII text um so it's one of the more popular objects you see you know at least with Powershell uh notice like can stop that's a Boolean object that means it exists in only a true or false state uh status is a system.service process dot service controller status object okay so that's literally the data that's in there it's that type of object now there are going to be situations within Powershell where you want to grab the object that's in the property value of the original object that's in the pipeline right so that's where the uh the last um thing that we could do is uh for each okay so we're going to use a command list called for each object now again some people don't necessarily get the purpose of it but it's it's you're trying to fine tune what you want um what you want to supply to to whatever you know whatever code you happen to be running right so like let's say for instance I was doing something here like with get service uh let's say I did name and then I happened to use computer name and I'm putting the name of a computer in okay so that runs right I'm not going to point it to another computer because I don't have any more on in my lab environment here so it's just this one or nothing uh but notice I'm supplying a value to computer name and if I go look at the help on get service uh you can see if for computer name that is expecting a string object okay so if you feed it anything else it is not going to like that so this parameter accepts string objects and that's it okay so where could this run you into trouble well let's say for instance I want to grab a bunch of computer names let's say okay and so let's say maybe I'm doing a query out of active directory so get 80 computer filter star you'll notice that this produces a bunch of computer objects right it produces you know grabs information out of active directory and if I go and pipe this to get member you could see it's producing this type of object right so it's producing an 80 computer object okay which again has properties on it and notice you have a name so the actual names of the computers uh that is existing in string format okay system dot string right so let's say I wanted to do something like this um so I want to grab the computers I want to grab those two services but I want to grab them from a bunch of computers so maybe you're like hey I know how to do parenthetical Expressions you know something like that right and the problem that's going to happen when you do this is it's going to error out because this code here is producing and 80 computer object and it's being fed to this parameter which accepts string objects so what I need to happen here is I need this code in here to Output string objects because this parameter only accepts string objects okay so how do we go about doing that well what we could do is we can let's let's fine tune the code here before we put it into the parenthetical so let's say again here you know just running it on its own it's producing the 80 computer so you might be thinking well we already know how to do that right we could do select right and that's kind of not the the hill you want to be going you know up here so if I do select object and I say property name um I do get the names of the computers in question but if I pipe this to get member you see I still have the 80 computer object in the pipeline I just removed all the properties except one on it okay so that's not what I need to do here what I need to do is I need to take the AED computer that's in the pipeline grab the object that's in the name of property and put that object in the pipeline replacing the ad computer so I want to you know take the Ed computer object get rid of it and in its place put the string object that's in the name property right so I can do that by using a commandlet called for each object so for each object we're going to use the member name parameter on it and then I literally just type the name of the property that I want to grab the object out of so in this case that was me right so I run that and you could see the output here notice between selecting and doing for each you'll notice the output's a little different see how in selecting you have a column header uh kind of showing that you know name is a property of some other object that's in the pipeline like the 80 computer object and here notice you just have the raw values and in fact if I were to check this by piping it to get member you'll notice that what's in the pipeline now is string objects okay so now this code right here is all set to be put into that parenthetical expression okay that I had okay so this would run perfectly fine so let me bring up that uh that one that failed before like right here okay and we'll instead put the get 80 computer filter star pipe and then for each object member name name right so that's grabbing uh those so this is outputting string objects which are what was in the name property for the 80 computer object and presenting those strings over to computer name so this is still probably not going to work because I don't have all of these computers turned on but we'll just hit enter and give it a shot all right but notice it didn't error right away all right so that's basically proper syntax in here okay so now um this could have a fair amount of abbreviation done to it by the way okay so for each object has an alias of just four each or um or percent by the way the percent signs actually an alias for this as well and member name the member name parameter is actually positional as well um so if you just want to uh just type in name it'll relay that to the member name parameter if you will so uh so I'm going to hit I'm done waiting for this here I'm going to hit Ctrl C just to kind of exit out of this or maybe not come on that's not working here all right so so let me uh need to wait for this to finish or what it's trying to do is it's timing out and all of the uh the three other computers I don't have turned on here so my apologies on that let me pause the video for a second I'll come back uh when this is finally finished uh timing out trying to contact the computers that don't exist right now so bear with me one sec okay so sorry about that I was able to get that uh canceled out um so bringing back up the command here um I mentioned you could do some abbreviation to it so let me uh let me kind of show that here so for instance with uh member name um so what we could do is we could uh not use member name and then for each object as I mentioned you could use the Alias of just four each um or you could actually use the Alias of percent so either one works uh which is kind of weird right so again let me let me show each of those let me show each piece without running get service through here so let's uh that in and you can notice you know you can see that that worked and again if I just use uh for each okay that can work as well all right now you don't have to limit yourself just to Name by the way this can go grab any property that's there so if I kind of you know bring this back over to get member 80 computer like notice there's DNS hostname as another property so I could a new I could you know grab the values out of that as well so if I did four each um DNS hostname okay that'll grab the values that are in there right so again kind of the purpose of for each when you're using it against a property is it grabs the objects that are in that property and puts those into the pipeline replacing whatever the original object is there so basically what's happening is we've got the 80 computer okay and by using for each I'm grabbing the objects like in this case that are in DNS host names I'm grabbing those string objects and putting those in in back into the pipeline replacing the 80 computer object that was there all right so now another thing we can use for each four is uh calling methods all right so you could if I happen to look at like let's say I do get service and maybe we'll look at one service um so notice it's stopped and if I pipe it to get member um we could see the bid service is currently stopped and on the service controller that's produced by this uh you will see that there is a start method all right so so the last thing we can use for each four so if I do uh for each object uh member name start uh the fact that I'm using the name of a method right um we'll call that method so that's uh another fancy word you might hear use is you're enumerating a method so that'll call that method and now if I go back and look at the service you can see it's it's running there so that could be the other use of for each object is uh calling a method okay and you could also separately Supply if you need an argument list you could Supply an argument list if you needed it as well uh so start does not need an argument list so that's why I could just use member name and away we go okay so that would be your second use of for each object so for each object can extract the the objects out of a property putting those in the pipeline and then for each object can also call methods on the object that's in the pipeline Okay so so that was a kind of a review here and a quick look at uh using uh formatting using selecting and using forage object uh any questions please hit me down in the comments uh and if you did enjoy the content please consider liking and subscribing um helps the uh the algorithm and makes my video show up in more uh YouTube search results so would appreciate it and uh with that have a good um good one everyone and we will see in the next video
Info
Channel: Chasse TAC
Views: 851
Rating: undefined out of 5
Keywords:
Id: p5Lj2pFehDs
Channel Id: undefined
Length: 38min 34sec (2314 seconds)
Published: Fri Jan 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.