PowerShell PSCustomObject - Custom Object, the way it was meant to be.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
powershell custom object what is it and why would you want to use it while using powershell let's have a look [Music] hi and welcome my name is kaine potushin and i've been making videos about powershell for over a year now therefore if you are powershell using any final capacity please subscribe because there is more and more videos coming about the tech as it keeps getting me excited for the past 4 or 5 years now and today's video marks a bit a little bit of celebration because we've passed a mic of 500 subscribers which well i didn't really anticipate that will happen you know i thought that would be a another boring channel about a guy talking about technology and others so thank you guys for that but here coming back to the actual powershell custom object let's have a look what is it and why would you like to use it and i tried to look for the official documentation which surprisingly wasn't really that great so i googled surgery a bit longer and i found actually a great pose by don jones on powershell volume and if you haven't heard about don jones it's the guy who wrote a couple of really great books i mean more than couple but probably the the ones that actually really get you going with powershell in the partially amount of launches so if you like to read and learn check his books but anyways let's look for his pose on the forum ps custom object is a type accelerator it constructs a ps object but does so in a way that results in a hash table keys becoming properties ps custom object isn't an object type percent it's a process shortcut the dogs are relatively clear about it and here's the link ps customizing is a placeholder that's used when ps object is called with no constructor parameters and what does it really mean to us that means that we can pretty much build it or construct and create ps calcentric as we will create a hash table by having all the all the joys of having a powershell object with methods with access to properties and etc etc let's actually dig in how to actually do it in the code okay so here's the script and if you would like to download it it is on github check the description you i will post the link there circle you can download it and follow along yourself because i will be literally doing everything what's in that script on that video so we just you know presenting it and explaining the way so how we actually create a powershell custom object we literally if you look at this part and if you've seen my previous video hash tables that construct we probably look very familiar to because we obviously declare our variable then we do add sign and we do curly brackets with my property name and the value in it isn't it the only difference here is that at the front of it we add ps custom object in the square brackets and this is our this is the type accelerator i mentioned bit early on so that pretty much says i want you to build this object with this properties that's literally what it does what it says this is a shortcut there's a longer way all the way we could use for the new object but i'm not even gonna bother to show you this because this is modern this is preferable way of doing that so let's build it so i ran this and at the moment the details are here about my pretty much cut mr fiber he's well usually he's not on this video so i cannot show you him but i promise if he ever be sitting next to me i will show you my cut but now we going so we created the object and when you actually run it probably straight away we can see a defense that actually rather than having a bunch of properties on the left and the right now they are formatting that way and that's probably seems more familiar or you when you were using normal powershell command blitz and this is the and this is why because this is the object and we can access that properties pretty much like we did with hash table so i'm not gonna talk more about that okay but what we can do for example this we can because this is already an object we can straight away convert it to other property types here when we work with powershell i don't know even importing data from sql on text files from anything we always always usually strive to get them into the object because when we have them as the object we can actually do manipulation we can work with it and once we're done with this work we can actually go and export it to the other format like json or csv files on etc and exporting to json is very much preferable let's say of a csv because you can we can have nested properties like with hash tables and json suppose then you will just create another array for us why csv won't because it's flat so you will actually see under the value we see something like system.objects or something like a type name that actually it's there it doesn't resolve them properly so yeah so for example what we can do we want to save let's just create a sample path and now we're taking our object we're piping to convert to json depth 99 and i just put it very high number here for the reasons if we will have a object with multiple nested properties they at some point i don't remember what's the default but something you would stop resolving them so what we really want is to just grab all of them so i just put that high number you often don't need to do that but in case you will see with type names values other than actually what's supposed to be there that's probably because def is not high enough and then after that one we just saying go and save it to this file we just we just specified so let's try that one and now let's try to import it so we just import we've got content just to show you this is actually a json file and we can see that this is certainly a json construct yeah we have kelly brackets we have properties in quotation marks and we have column and then followed by another on the value followed by comma and etc etc so what we can do now because it's json and powershell has a built-in command for json can just convert it from json and that's how it's done so in other words json is probably internet and apis the one you can do that you can pull information from api and read it and have it in powershell as well but okay let's move on to properties so obviously we can access properties like i just did before so we can actually do it that way or we can dynamically specify them so if i for example create a variable called prop and then type and now we ask for object dot prop which in that case will be type so i can absolutely dynamically create properties and resolve them what else and that's probably where the first like a major difference comes between hash tables and powershell custom object is that if you want to actually add more properties we need to do it in that way we need to pipe it to add member i'm going to specify the type and because we can have multiple ones in this video i'll be showing you node properties which are literally values per se but you can also have methods we have aliases that will be properties bringing analysis to another property so there's something that is not user friendly to something more user-friendly or etc you can use aliases then we specify the name of our property and actually what's the value and yes i know we shouldn't have properties with the space behind them but i just want you to show you that we can have them in case we have to it doesn't often happen but yes sometimes you have to have them that's for that's how we can do it so let's try out and just and now let's just try to attribute and certainly you have favorite snack dental treats yeah my cat loves them now a word of caution if we already yeah we already added that property if we look at this object we have no favorite snack and now if i try to add another one our show froze yeah you cannot do that because you already added one so it might be especially when you're scripting or doing something it might be worth checking for that condition because you will throw you will complain and how do we then remove properties we need to go for the scope concept of ps object and funny thing is because i can actually show this if i go for object there's no ps object isn't it if i go through get member there won't be ps object as well by default it's hidden so we can do something like get member pause and false pretty much will show you all the properties now even the ones that have been purposefully hidden by the developer so you can access them in the sps object so pretty much and once we are in this part you see it doesn't happen i try to tap it doesn't tap i try control space nothing happens because it's hidden but if i get that then i can actually start resolving things so something to be well that sometimes things coming from nowhere or you can't find them well maybe they're hidden you can then just do get member hyphen force but okay so in that case we go for objects then it holds another property called properties and now we can just call a method called remove and rid of it so if we try now it's not there and it doesn't complain yeah so something to be when maybe when you do your if statements or checking is worth checking whether actually the property exists rather than if it holds any data because if i was just looking or if it's there maybe if it's holding data there might be difference great and how we can list all the properties well there are a couple of ways one is pretty much going after this concept i showed you so you go object ps object properties name the listed or if you rather go for the get member you can specify that one node property you can just expand on the name so a couple of ways i would imagine we you prefer to go for the shorter version well we see okay so now as i said hash tables are pretty much very similar to ps object and why would you want to use one or another the main difference is from practical point of view that hash tables are faster are faster for accessing data because in hashtable you can just go and say give me property with this value while in ps object we need to use things like where so into filter them so you might have instances where you have an object already and want to convert it to hash table i actually have a hashtag we want to convert it to powershell objects so how can we do that we can for example so we have a hashtag already here so just create it but my hash table is we see name so that's pretty much how hashtable works we know it's hash table so what we do then we can pretty much just cast it into in we can create new object and then cast it into partial custom objects so let's try and it just does it so it's fairly safe for what if we already created hash table upfront we can just go and convert it however the other way so if you want to go from the powershell object to hashtable is bit more complex and we need to loop so what's going to happen at the start we just declare a blank hash table and then and if you are yeah again if you haven't seen the video about hash tables it might be worth for you to catch up on it so just check this link and then what we do pretty much we grab all the properties property names from the powershell object and then we just created so we create that saying that the new in in my hash table the property name equals powershell property value and that's way how we can create uh how we can quickly cr convert from our custom object to hash table so let's try and certainly it happened so it's you know you can literally just copy paste this example and use it it will work for anything as long as your hash table name and your a new object name matches so what then now one the thing that we can actually do we can have methods and methods is the concept that when you have your object with certain data methods allows you to do something but there's some action yeah other than just storing data you can actually do something about that data directly from the project itself it could be as simple as adding values or displaying voice you can do something with mobile's pretty much something if that's gonna happen it's gonna make it more it's gonna make it dynamic or change something or return something etc so for example what we can do we can have this little method and as as you can see i'm declaring this in the curly brackets so in that case it is a script blocked yeah script block is anonymous function in powershell and pretty much we just say hi my name is and this name and i like to sleep so pretty much this is a special variable that tells the object just use this object you use value from this object in that case that'll be our name so for example if the name is fiber they will say hi my name is fiber and i like to sleep okay so remember this is a special one that we refer to this specific object cool so let's see so what we do we're going to pretty much add another member but in that case that's going to be a method yes script method before we use script property we're going to give it a name and say hi and then the value will be that method we just created it could be longer it could be more complex but i just like to keep things simple so let's add it to a current object of mr fiber and now you see i'm going object dot set high and then brackets and that's how you call methods patterns in the brackets and in brackets if there are any you pass in parameters if they are not binders you just put brackets so let's try it and certainly hey my name is mr fiber and i like to sleep so if we go for object we go say hi go that way we can see that this is in fact a method or if you let's say we'll go now through that way we can see we see that icon that icon means it's a method so certainly powershell is aware this is a method it works and i can now have my object and i can actually straightaway process that information if let's say i don't want to write external function or something like that i can have it in the object set so we just have this example about converting powershell object into hash table but for fun we can actually have it this shipped with the object itself as the method isn't it so i can just go my object go and then convert it to out as a hash table yeah or two hash tables i can just have it as the method so again we that's my parameters method that's the name and now we see the value is now i'm creating the hash table and then put the hundred game like before this ps project name for each because i'm in that case i'm using a method for each and now we're saying the hosh the hash and the hash is in that case there this will be the property name equals this property name that's pretty much it and then we've done it let's try that that's my object i mean that's my method added to the object isn't it so now we have the two one is say hi the other one is out hash table let's try this and there we go so if i classical object i have a so if i just call object i have a my object but if i go object dot out hash label it on fly created on the hash table so i don't even need to write this i can just have it there and obviously i can still call my say hi my method so i can have multiple of them attached to one object great types so what is our type because we have powershell type type is what specifies what this object is how it should behave it's true it should create your kind of contract that if let's say this is a the type is string like a text and i can go and convert it in certain way i can go past things into it let's see if it's integer so it's number then this type will then allow me to pass these numbers or outside of the string etc or if it's something a bit more complex like id object you have certain properties etc etc in powershell however types are a bit more loose so i can kind of create i can create object with certain type i can use it as a type i will be loose so it's a bit more it's down to me really to make sure it always happens it won't be enforced but still gives me quite a lot of interesting things to do so first of all if i check my project my objects are my object we see this is pretty much system management automation ps custom object so remember when we created in the first time i put ps custom object in the square brackets that meant that there was this accelerator rather than me needing to type all this thing to create it i just specified this one but this is it so what we can do we can do type names and we can see this is ps custom object and it's object because pretty much everything is object as far as powershell is concerned or in that case next framework and now we can do this little trick when we go and we at the very top at the very front we add one more property called kp cuts are also you can probably figure ykp and this is just something i don't know very special name i wanted to give it so if i now go after adding that bit and checking see my type name has also so what does it mean what can i do with that and this will be a bit more now advanced concept but that's really where you can use it so in that case when i will declare the function and that function will take certain parameters and i can pretty much specify that i want this parameter which is cat that would be my type name that i just created yeah it must be this type if i will give you a string a number or anything you will just power to just go and say bugger off it's not what i'm looking for this is incorrect so that way i can make sure the way what i'm what i'm receiving as the function what oh in other words what you're giving me is of the specific type name and therefore i will know what to do with that because it's type you have certain properties and certain methods okay that's just a bit more end value from pipeline because we can pipe it but anyway so we declined function and in the function pretty much if i'm passing that parameter we're going to call my say hi function otherwise we're just going to say that cuts are awesome because they also so why not say that all right so let's just call it yeah so that's my function so i need to load this whole thing to memory and now let's just call here so invokes are awesome and pretty much that's awesome yep that does it i didn't pass it now i will pass in my object of my cards object which we now have already updated the type to cuts also so in that case it should call get that route so we have a cut and we have cuts awesome yep it does it and now i will just try to pass in the text here string of something and now look what happens it rolls because it's not of the right ps type name i have this control so if you write functions this is one way how you can ensure and you write your own objects etc that's one way how you can make sure actually this happens this is how you can enforce it it's a pretty cool concept because the other way at least i am familiar with is to actually have a purple c-sharp class which is a bit more well it's be the absolutely different concept this way we can fairly easily have that functionality within powershell itself all right so just for fun let's create another object another cat that his name would be wiscons and here at the very top i would declare it that this is actually this type name yeah because before you if you look at it before i did add it after creating the object and this one actually i'm just declined this at that point so let's try this and now i need to do pretty much my method again to it that's why i say as when i told you this would be loosely coupled that's why because i cannot enforce it at least i didn't find a way to how to enforce it but anyways let's say i am a good guy good scripter so i will pretty much write it nicely i will make sure it happens add the member and now we can see when i actually execute it it picks up the cut so certainly it works for my object and what else we can do sometimes for example in the active directory command list of sharepoint on networking there's a lot of information is displayed there it's hidden yeah you have some kind of information but actually when you go and grab volume at least it will give you a lot of more and the same we can do with when we have the type date here we have type name you can use this function called update type data and then pretty patina you can specify which one we want michael's awesome and then that just the properties of a name and type so in other words i will hide the species so let's try this okay so now the list should be limited just to these two properties and they are let's try another one certainly works because i specified of this type but if i do well modlis give me everything it gives me all so another nice feature if you want if you don't want to display all the information that is there that's how you can limit it without any doing things like select objects or whatever which might effectively change change your data and now because i had a that's pretty much would be like a fad but it's not official but part of it because we did arrays with the hash tables and i do custom objects i how we can build this all together and i thought that we can make a little i could write a little wrapper that we utilize effectively our genetic list you haven't see i didn't purposefully include that in the hours lesson because this one this type pretty much requires you to specify what are you passing in in the list yeah when we created array list you could just create new ios and just throw things at it it will take them why when we use generic lists we need to actually specify what is coming in we could just say object by kind of defeats the purpose and i'm showing you this one now because i list have been deprecated for quite a time and well they've been deprecated are still working fine so there's no nothing wrong with them but in case you want to actually go and take your powershell practice step further just try to read about this read that because pretty much the console is very simple very very similar system corrections generic list and then in brackets actually what you're passing is string hash table whatever all the other type is done and we're just creating a new list yeah that could be as well an array list if you wanted or even an array but we don't as we know we don't do things with that in our because they're not designed for adding stuff but anyways that's my list and that's my location so that's just some a few cities i've went through throughout the world in my life why traveling was permitted well it hasn't been that easy for the last couple of years but well let's see and that's pretty much where we're going to call and this is api this is this api pretty much gives you however for free you don't need to you don't need to sign up to anything create any keys and it does it by defaulting actually in this ascii format actually you know you draw your little sun etc so it's fun to work with but also very simple case for us so what we're going to do we're going for each of these cities check the weather and by hitting this api then followed by followed by the city name and then that format pretty much has it i want this as the json because by default it gives you this ascii pictures etc which will it's very hard to process this is just json so we're going to hit that one and when it happens it retains certain properties so for one that's where we actually start doing our custom object yeah that's the construct that you will see very often that you create an array oh sorry that you create an array up front and then you will look through your items you will then once you've gathered all the information together yeah once you have all the properties from all the sources in that case we just have one source there could be more of them then at the very end you will combine this all occur like a one powershell object yeah with all the properties i want so this is something what either i will pass on to the next function or it's something i can actually return for the user so what we'll do we specify the city then we grab the temperature and as we can see this came from data which is what's coming from the from the api and that's pretty much just the properties i'm using for that so i have temperature we have pressure and then once i've constructed this thing i'm adding this to our our list to that one and once everything is done i'm just displaying this list once so i know this maybe it's familiar maybe not but i quite when i had to do it like a phase or second time i was already actually confused how do i actually do that how do i take multiple things and how do i actually build them into list i could make a little shortcut and actually just rather than adding to list straight away displayed but in that case if i wanted to take that output i'll need to actually go and explicitly put it in the variable and it's much nicer to actually work with the leads with actually having an array okay so let's highlight this all and run it so we see what's happening pretty much checking weather for the city and this is just how it builds the thing and if you are if this concepts in your family check my video on the building strings this was actually part of the whole course of powershell but this is pretty much what happens here and once we have it let's display it and there we go so that's how we do we have a city we have temperature and we have a pressure now we can see london pressure is fairly low today that's that's that's why i haven't been paying hundred percent today but yeah anyways it's just the high pressure so nothing wrong great looks like we've done it well done let's sum up there we have it powershell custom object i know i went slightly more advanced but because a lot of concept was similar to hash tables i didn't want to literally repeat myself provided you were the hash tables and maybe you know actually i showed you something that you can use in your scripting practices so that's kind of summarizes or concludes my series on on on list and arrays and hash tables so i will need to pick a new topic if there's anything particular you're interested in you know powershell or maybe in 19 general provided i know about that uh let me know leave me a comment oh that's that that might help me i will see because i think because i passed this 500 subscriber mic i can actually now start interacting with users a bit more and by this i mean i can create posts or polls on the youtube so let's see if i can do i will probably throw something but yeah if there's anything in you you're interested in please do let me know please leave leave a comment because effectively i'm i'm recording this but the idea is actually i want to bring more people to powershell to using them because the more people we have in the community the stronger these languages the you know the more beautiful things will happen to it and you never know you know let's say like if i'm looking at my example i wrote a lot of the code and never really i never really got involved into any open source project but i can contribute that way i can actually make this vid video so hopefully i can help others people i can bring you along uh so you never know maybe you will be just the user maybe you will contributing so but i already know what i'm trying to say say the more users we have in the partial community the stronger the community is and also i know that just because will starts using it you start writing working more with code we might actually make the general it it community stronger because suddenly that opens your way more dogs so please do let me know you know leave this like or the dislike however you're feeling and i hope i see you in the next video have a great day see ya
Info
Channel: Kamil Pro
Views: 7,521
Rating: undefined out of 5
Keywords: powershell, pwsh, object, custom, pscustomobject, hash table, data, structure, 2021, course, free, windows, linux, core, scripting, type, pstype, class, array, github, method, programming, coding
Id: DAVGyCytsqM
Channel Id: undefined
Length: 28min 6sec (1686 seconds)
Published: Tue Nov 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.