PowerShell Fundamentals Add-Member Cmdlet

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mike and welcome back to the channel if you are new to my channel and enjoy the content uh let me ask up front if you could uh please like subscribe and comment uh really helps the growth of my channel as I am just starting out and looking to uh to grow this thing so in today's video this is going to be in response to a um a request from one of my viewers um asking about a couple commandlets a group member and uh no group object I'm sorry an ad member uh so the group object I'd put out in a video last week and so I figured I'd throw one together for add member this week so we're gonna have a look at what this commandlet does and hopefully this will be of interest not just to the uh the person requesting but also anybody else out there who is uh tuning into my channel so with that let me uh let me flip over to my desktop here and we'll go ahead and uh and get started so typically if you're first starting out with uh with a commandlet uh that is let's say you know one that you're not all that familiar with uh usually for me my first stop is like looking at help right so I've talked about help in uh you know some of my Powershell basic videos and with help you can you know look up uh whatever the name of the commandlet you're looking for so in this case I have ADD member um and I can at least pull up so I can see what the rough syntax looks like so you could also open up help uh the online version of it which would open up the the add member page off of Microsoft's documentation site and you could also do show window a version of help which would open up um another Powershell window which would have that documentation in it so if I wanted to do something like help add member I'm going to use the show window parameter so what's nice about uh the show window way here is you're not relying on internet access for anything um it can pop open the help and you can see a lot of decent information but more specifically like you know the individual parameters but then uh the actual examples themselves here right so and that's where in a lot of cases I usually first start when I'm looking at something as I usually pull up the examples and see what they have you know so so some code describing what it does um and if the formatting is not your cup of tea the online version of the page that Microsoft keeps on their site is a lot better formatted uh from from Reading if you will so you can have a gander at that all right so anyway so long story short you know what is ADD member up to so add member commandlet lets you add members uh so properties and methods uh to an instance of a Windows Powershell object right so so let's say first here I have uh my folder my prompt currently set at this folder location and if I use uh get uh child item that'll go ahead and list out the files and folders that are underneath it so this is basically one of the working directories I use when I'm teaching a class and so for instance here you see there's like a bunch of files uh some folders here so let's say I just want to mess with the file so get child item actually does have a filter where you could tell it hey I would just like files so I go ahead and I get that output and let's say you know like we want to work with the object that was produced right so remember in Powershell whenever you run something and it produces output it produces it in the form of a.net object and if you want to see what is available on that.net object you can take whatever produces the output and pipe it over to a commandlet called get member uh which will open up a output that shows What DOT net object was produced by the previous commandlet so that's all getmember does or it's Alias of GN the letter is uh G and M right which is normally what I type so you could see for the output that was produced up here it is creating a system.io.file info object okay so that's what we have to work with and we could see on that object that there's a bunch of methods methods represent actions you can take against an object and there's a bunch of properties straight out of the.net framework so a lot of these uh when you see properties and methods this is literally coming straight out of the.net framework okay like this type of object class and it is defined in the.net framework of having the methods which is actions you can take against that class of object add properties meaning facts individual facts about that class of object right so for instance here like I could see there's a length property so that denotes like the size of the file in this case I mean you can see of the methods here there's methods for open read open move to replace so these are all like file actions that you could take either yourself or you know file actions that the operating system might take like when you're using Windows Explorer or something like that all right so the point of get member I'm sorry good member uh the point of add member is you actually want to add things on here so maybe you want to add Properties or you want to add methods okay so ones that are added after the fact um you see like like right here these note property ones these were actually added in by running uh this particular commandlet here get child item so some of the commandlets in Powershell they don't change the.net object but they can add extra stuff onto there so you see here like there's some script properties there's some note properties things like that okay so so you can always add on to the current instance of your objects so by using add member you cannot go back and change the.net framework that's not allowed but you can change whatever objects live in your Powershell pipeline so that you can you know tack things onto them if you will right so some of this might mimic behavior of using like calculated properties when using select object okay that would be another way of potentially adding things on there but the limit of calculated properties with select object is you could just literally add properties whereas with ADD member you can work with the overall membership of an object not just the properties but also the methods as well okay so the first and probably easiest way to kind of play around with this is just to go and add a note property right so if we if we go back and look at the help for add member oops spelled that wrong for add member uh you can see that there is a property set here of note proper note property name and note property value all right um and also notice uh you know you could Supply it input you can specify type and pass through would be the other parameters so let's start out uh with that okay so so we have our Command here that produces the output so get child item and we're looking for just files so again that's what that produced right here and now we want to go add something onto it so we'll pipe it over to add member and let's say we'll do a note property uh so note property name um how about I just again I'm I'm making something up here so how about I just call this like type and then the note property value I'll just give it a value of files so basically like what type of output is this so it's files output okay so if I run to this it doesn't seem like it does anything and it did actually do something don't get me wrong uh but add member when you just run it like this it doesn't send the work that it did back into powershell's pipeline right so basically again how powershell's put together is if you have multiple command Lids like linked together via pipe uh the first commandlet whatever objects it produce it puts it up into powershell's memory what we call the pipeline and then passes it over to the next command lit through something called pipeline parameter binding okay so it kind of matches up and sends that information over so then this commandlet does its work and if it is configured the let's say the I don't want to say the proper way but if it's configured a certain way it will return its objects back to powershell's Pipeline and if I don't have anything else to pipe uh no other command list to pipe to that's what you see is your screen output so basically the screen output is not caused by an individual commandlet it's caused by objects left over in the Powershell pipeline once you have run your entire command line so in this case add member by default does not return any objects back to the pipeline however you could force it to do that okay so I could use on this I could use the pass-through parameter which kind of forces whatever objects were modified by this to return back to powershell's Pipeline and then that would give me my screen output so I'll go ahead and run this and you'll see that happens now unfortunately the output looks exactly the same right so add member uh it did add this property on a property called type with a value of files now you might be thinking well I don't see it so what did it do well remember that we're not changing the type of object that's in the pipeline and if you're using a popular commandlet and Powershell there is a lot of default formatting rules that are present within Powershell that tell it hey if this type of object is in the pipeline like in my case a file info object we want it to Output as a table or list with certain properties and so that's what's happening here if you look at when I did the get member you know you could see all of these properties right but if you look at the default output it's only showing one two three four of them okay so Powershell is making a decision on that uh so that is uh uh a piece of well not a piece of but a a way that Powershell does output called default formatting so when there is an item in the pipeline an object in the pipeline and Powershell has to display it to the screen how you know what are the rules that it displays it as so you'll see a lot of this default formatting with output for a lot of popular commands like if I run get uh get service let's maybe do name bits so what made it you know display status name and display name when uh the output forget service the object has like a dozen properties on it again it's it's default formatting uh same thing if I did like uh you know get process you know what made it decide uh to do a table with you know these particular you know properties in there by uh because get processed if I you know pipe this to get member you know there's there's a lot of properties there okay so that's all default formatting right so in order to see what you made um so like right here I don't see what I made I don't see that new property you have to know how to pull it out okay so one way to do that is to take your output and then you know using pass through uh then pipe it to select object where I can then specify what properties I want to keep on the object in the pipeline and thus display on the screen here so maybe I want name uh length and my type property that I just made all right um so there we go so we could see for the output there's the name of the file there's the length of it and there's um the static property that I just put in there that note property all right now if you did if you did not do select object you could also to see that it's there uh pipe your output again pay attention to my cursor right down here I'm just going to pipe it to get member and notice that there's now another note property added on of the the note properties called type and the value inside of it is files all right so that's one way we could add something in here okay is by doing a note property name and a node property value now you might be thinking well you know like I could see this being handy maybe but you're just adding static names with static values there's not really more to it and with node properties you're absolutely correct okay so so there's not a lot of logic I can put in I'm just basically blindly not not blindly but I'm just basically adding a property and a value onto every object that was in the pipeline so you know like if if this was you know if this output if this initial commandlet produced one object add members modifying one object if this produced you know 10 20 30 objects add members modifying all of them right the exact same way all right so that's what's happening there all right now what if though I wanted uh to modify everybody the same way uh but I didn't want like a static value like I wanted to put maybe some logic into this okay so one of the ways I might Envision this is like let's say if I just kind of look at um the get child item to start with here and I look at the output and I have all of these files in this folder and they all have a link now for me I know that the length is size in bytes okay that's basically how it's measured how a file info object operates however someone else might not so maybe you're writing some code that is going to be run and show output for someone else you know on their computer and maybe you don't want to see a length property maybe you want to see in like you know maybe we'll call the property size and then do work on it to convert it to maybe a better unit than than bytes might be all right so so let's say here um we'll go ahead and run add member now this time I'm going to use the member type parameter because I want to do something other than a note property okay so no property is cool it has its own parameters but there's lots of other uh member up member types you could add in so we could do an alias property we could do all we could do code method code property Dynamic event member set method uh note property um and the one I'm looking for is actually a script property because I need to do a little work to turn to this from bytes into something else right so that's what we're going to do I'm going to give it a name and I'm going to call it size okay just because the one there is length so I might as well call this one something different and I can maybe even do size KB let's say I'm going to you know convert these to kilobytes and then we need a value all right and the value is going to be a script block okay so basically if some code inside of curly brackets now if you want to work with uh the objects that are produced by the previous command there is a variable for that it's called dollar t h i s dollar this uh mimics a lot like what dollar underscore or a dollar PS item do um as in they reference the object in the pipeline the difference here is we're not working with the object per se we're not changing the object we're changing properties and methods about the object so I don't want to use dollar underscore or dollar PS item okay because that references the object itself instead you have an automatic another automatic variable called dollar this all right so that this variable you'll find that in other programming languages.net specifically so that's what I'm using here so basically I want to take the object in the pipeline with dollar this represents do a DOT uh grab the data out of the length property and then divide it by 1 KB which in Powershell that would equate to uh 10 24 1024. okay so that would go ahead and do its thing and I'm going to use pass through so that this outputs the object back to the pipeline all right so I go ahead and run it um and oop I missed something what did I miss hmm oh I did the wrong math operator all right so I did a backslash instead of a forward slash my band so forward slash is the Divide symbol all right let's run that again okay and there we go now again same problem right because I didn't change the type of object so it's still doing its default formatting for the output okay so it doesn't care that you added a property on uh it's not configured to show that property by default so in order to see it I would still have to pipe this over to let's say select object uh and let's do property let's maybe grab name length and size uh size KB because that's what I called it add notice there we go all right so there's the name there's the length and there's the size in kilobytes all right now the uh one of the the downsides of Powershell when you do math operations like this is you might get you know like a whole bunch of decimal numbers so what you could do just as a quick fix is inside your script block where you did dollar this divided by 1kb is come back in and cast it as an integer which would be a whole number all right so and then again I'll do the select object to then draw it out and there we go so there's my size in KB these guys are all one there's four kilobytes there's 44 kilobytes and so on all right so another way that we may wish to uh mess with add member here is maybe to work with uh work with variables all right so so let's say for instance let me um let me start with let's say maybe we'll do like dollar dollar test uh equals my get my get child item uh looking for files all right so now that has my file list so now if you do something um against it uh with add a member you're kind of writing into that variable okay so you are making changes in there right so uh without needing to use pass-through all right so let's say for instance uh let me pull up just what's in dollar test right now which again is just the output of this folder and how about we do something like this uh let's do another script property but this time I want to add a property uh that is based on the date so I want to add a property let's maybe we'll call it age and I'll do an old or a new so I'll put a value of old if the file is older than a year and I'll put a value of new if it's newer than a year all right um so let's do something like that so let's say so first of all how do I get that okay so so if you use the commandlet uh get date that will always that uh get date that will always produce uh the current date and time of when it's run and this is exactly when I'm running this you know Sunday February 26 324 uh and I'm stuffing this video in uh right before I'm gonna you know go watch some uh go watch some TV the Nascar race starts at uh 3 30 uh eastern time here so so that's my uh my deadline there so 3 30 driver infos whatever um race will probably start about 3 40 3 45. ah so anyway uh so now if you take that uh and we'll throw get date in there so now I have that in a variable I can manipulate that date object that date time object using methods on it like add years and do like a minus one okay and that'll push the date down to what it was a year ago all right so that's what I want okay so basically I'm going to take that ah so now let's uh now that we have that in its own variable here so again you know I have that defined let's go ahead uh bring back up the dollar test right um and we already have that defined so we're going to pipe it over to add member and in this case the member type will still be a script property uh name we're going to call this age and then uh the value uh and then we're going to have our script block right so now in the script block I'm going to put some logic in here I'm going to use an if construct so I'm going to start this out with like if okay um and in fact let me uh let me carriage return this just so ah never mind uh will you let me yeah there we go okay so let me carriage return that so I should just be able to continue the line here so that way the whole like if construct is all in one line here should be easier for you all to read so let's do if and we'll say um a dollar this dot uh last right time right so that's uh so that's a property that's there so dollar this dot last right time is less than dollar date dollar date dot add years minus one okay so so this is uh so there's my outer parentheses there encompassing the whole if and then there's that argument on that method for the date time object so if the date on the file is less than the date a year ago so that means the file would be older than a year uh I have a script block that I'm going to select the value of old all right so that's what I'm putting in there uh then we'll do an else and a script block of new right close that and then finally One More close on the whole value uh script block there all right so basically again remember how an if Works uh if the comparison you're doing here evaluates to true this script block is going to run and if you have an else this will run if this comparison evaluated defaults right so basically like hey if it's uh files older than a year we write old to the new proper remaining making called age and if it's not we'll write new to that property called age right so bazel will make an age we're just controlling whether the value is old or new right that's all we're doing here so let's go ahead um and let's do pass through um no actually we don't need to do Pastor we're adjusting the variable my bad all right so I forgot we're working with a variable here okay so let's go ahead and do this and wow that was uh interesting okay uh let's see cannot add a member with name age because a member with that name already exists to overwrite the member anyway add the force parameter oh okay oh I wonder when I was messing around with this right before I started the video if I if I might have done something so let's uh yeah that might have that might be what's going on here because I I've had I had this Powershell session open here so let's just change this to instead of age um how about uh how about what is time change it to something like that right uh there we go I could have also run to this and used Force but you don't typically have to do that if uh if that wasn't already there all right so now let's take test and we could see on the output we still don't see it right because again the new property I'm making is not part of the default output so I still got to draw this out so with test if I pipe this to get member I could see that we're actually in there okay so there's time oh yeah there was age so that was a predecessor from earlier here uh but yeah so there's the time one that I just created and now uh if so that means I could do something like select object add property let's maybe grab name um last right time and how about uh time right and we can see here there's our new property that we added um and notice on the files anybody that's less than a year is new anybody lose more than a year is old all right so anyway so there's new there's old and so on so that allows me let me bring this this piece of code back up here this allows me to add about added add a property and then in the value field have some type of logic for what a value goes in there okay so that's um that's what we could do here all right now you could also uh to do a couple other ones here uh you could also do this inside of a script as well so so I'll kind of demo that um in the next in the next couple examples here so uh so for that I have uh the ISE open and I have one of the scripts that I created uh when I was doing I think one of the build a script series scripts so basically this is uh how to query a remote computer and I'm making my own object as output right so so this is somewhere in the build a script series I think maybe uh episode five of that where I put this script together from beginning to end and what's important to note here is I'm building an object for output right because that's what add member works with it works with objects so I'm I build the the property table up and then I kind of add it onto an object that I'm creating here so basically like if I go ahead and run to that script which was what the heck was the name of that script uh PC query right so let's say I go ahead and I run PC query and I need to supply the computer name I'll just use my local computer here so I'll just do localhost it produces this object as output so if I pipe this to get member uh I can see it's this PS custom object and you know here's the properties that it's putting out all right um and notice here kind of they're all note properties like I've added all of these in right because this object certainly did not come from the.net framework so it doesn't have straight properties it just has note properties which means ones that I have added on all right so now what can I uh do with this oops excuse me right there uh so what can I do with this well if I go into the the script here but you know before you make before you output the object to the pipeline you can do some work with it so one of the things I I usually do in scripts is I don't uh just leave new object on its own uh because once it completes it kicks whatever object it makes out to powershell's pipeline so instead I normally like to capture it in a variable and uh and then I could do work on that object before I send it out to the pipeline so that's kind of the first change I'm going to make in here I'm going to save the new object into a variable and then we're going to work on it if you didn't want to work on it by the way and a lot of people write scripts this way you could put the new object you made in in a variable and then immediately kick that variable or output that variable to the pipeline by doing like a write output uh dollar obj okay and notice if I make this change to the script and I run it uh it looks exactly the same all right so you just kind of added a step in there make the object put it in a variable and then output the object okay so why I put that step in there is because I want to mess with that object before I send it out to the pipeline right so what we're going to do here is let's say first of all let's do another type we'll add like an an alias onto here okay so we'll take uh dollar OB dollar obj and we'll add member and in this case the member type will do an alias property we'll call the Alias property how about we do PC name and then the value will use the computer name property that's already there okay so an alias property is just you're doing a a another name for a property that's already there okay so that's what we're doing here and again this is uh this does have um examples in stuff we we use in Powershell all the time so for instance like if I did uh get service uh name bets and pipe it to get member uh a service controller object in.net uh the name of the service is called service name however uh the makers of this commandlet decided to add an alias property on called name which isn't its own thing it's literally just referencing service name you know which already exists okay so that's what I did in my script so let me go ahead and run the script again and notice uh I can see you know there's computer name but there's PC name as well okay and it has the same value so I don't have to do any work in that you're just basically making uh another property called something else which is using a value that's already there so if I pipe this to get member I could see there's my custom object there's the computer name property which is fine and then notice here's PC name is the Alias property which is referencing whatever is in computer name all right so in that way if you want uh if you want to have two versions of a property one with a longer name and one with a shorter name you know you could do something like that so that's what aliases might be good for right now the last one I wanted to show here would be how can I add a method okay so so with this here uh dollar obj we could go ahead and add a method onto here now so let's take dollar obj we'll do add member uh the member type this time is a script method uh we'll give it a name how about we call this uh ping and then the value would be whatever is going to be in a script block here so let's say we'll do something maybe like test connection um and uh let's say the test connection um those test connection computer name uh dollar this uh dot uh computer name and maybe we'll do a count of one so it'll do like one uh one ping or one icmp there okay and in fact let me take value and just put it down on its own line so it looks a little better so I'll do a back tick right there uh enter and maybe tab this in so it looks like it's part of add member here all right so there we go so we now added a script method so let me save this and we'll go ahead and run the script again and again with a method just kind of keep in mind methods don't react when you're just running you know the script itself uh or the or the object itself okay so so the script is producing an object but we haven't called the method for that object okay but if I look at the output again through get member I could see uh that we should have and we don't because let's see what happened here um so obj at member and what am I not seeing here okay hmm try that one more time okay still not seeing it uh let's see did I save changes I think so oh I know what I did all right um I added this very foolish of me I added this after I outputted the object so I just got to get that line out of there so we created the object we added the Alias we we're now adding the script method now uh I want to do the output all right so yeah just goes to show you could always mess up your code uh so let's save this now uh let's pipe this to get member there we go there's the script method I was looking for and now so you might be thinking okay well how do you call that method just like you would for anything else remember your script is producing an object as output if you want to call the method you use uh you could use for each object that's one way to do it and member name the name of my method was ping and there we go so that runs the code that was inside that method which is basically just doing uh you know the Powershell equivalent of ping all right um so that is uh calling a method okay I'm not calling it but that's how you call a method yes uh but that's how you add a method onto an object using add member okay so now the for each object if you guys don't really do that too much um there's a lot of abbreviation that can happen with that for each object actually has an alias of just the percent sign and member name is a positional parameter so you don't actually have to have it typed out so I could you know make this uh abbreviated all the way down to just like that right so something like that all right so that's uh adding a method onto our object using add members let's bring the code back up so you all can see uh how I did the Alias property and the script method all right so that's going to do it for this video any questions feel free to drop in the comments again if you like the content please consider liking and subscribing definitely helps the growth of my channel and I'd greatly appreciate it and we will see you in the next video
Info
Channel: Chasse TAC
Views: 1,011
Rating: undefined out of 5
Keywords:
Id: kIQwreSA-rM
Channel Id: undefined
Length: 41min 58sec (2518 seconds)
Published: Sun Feb 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.