Filtering output - how to use Where-Object and Select-Object - [#6] PowerShell for IT Professionals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to the sixth episode of the course powershell 40 professionals where we learning how to use powershell from scratch today's lesson will be about filtering the output and selecting the data we actually want my name is camille and i'm the presenter here let's get started so so far in the course we were learning how to ask for mauler's stuff how to retrieve data then maybe how to pipe that data to files but what if i i'm after something very specific i really want to actually limit what i'm looking for what how can actually filter data one of the examples is if command let allows me to do that they might be actually the parameter and i'll be taking white cards like for example in get service name unless i want everything that starts with w and starts with b that's one dot but what if i don't know i'm i'm i know i'm just gonna use this commandlet once and i don't want to kind of learn all the possible parameters or what if i want something but is not allowed to be filtered by a parameter for example if i'd like to find all services that are running at the moment can i do that yes i can and there is pipeline and command let code where object coming to help so let's try it let me just run it first and then we're going to talk it through what it does how it works so if you look for that's this that obviously gives me all the services but if i pop it through where object and then parameter following this filter now it's currently bracket and now that will look probably very weird to you but i will explain you what does this a dollar sign underscore dot status hyphen equal and running in the quotation marks calls the curly bracket what's gonna happen see it gave me all services that are running only running so let's clear the screen and now let's break it down a bit because that's probably a bit of black magic what's going on here so for one we have y object and as you notice where object takes what you pipe to it and then we process it whatever we give it a filter script is one of the parameters that pattern states it what to parameter and how but what's a deal with ah and curly brackets because we didn't currently brackets usually mean you're going to do something you're going to actually action something here so in that case we actually will be running through whatever we pipe whatever it's in the in in the brackets so we're going to execute actually some comment here now what that is because you see it's even green dollar sign means whatever is in pipeline now so if you remember from the course before we have a pipeline here so i'm piping everything from get service to a object so whatever is being piped from get service will end up here it might be a bit i don't if it's still a bit confusing don't because after some time you will just do it really automatically just remember whatever i have in pipe on the other end will end up here so it's like i'll actually that's my get service and whatever its output output was dot means accessing the specific property so other than taking the whole object as it is get service and all the good stuff it gives us i will only like to focus on one bit and on one property and in that case it's property called status and now if you can probably notice echo that means it's equals and i will show you a list of operators in a second and not pretty much what kind of property i'm looking for what the value i like too much so in other words if you take status and and it does running then you take it along if it doesn't match so if it face that comparison then it will just ignore it you will just disregard that so that's how it works in more detail so let's get maybe two more so how do we know i like status well there are two ways i showed you get member commanded before and today it will come in action but let's just look on the get service and i just actually want the kind of first line so you see i unscrew sevis and it gives me status here so that's what i if you recall from the command that before i did get service dollar sign dot status equals running so it was on that property the other way we can prove it's there is if we do get service and we do get member all as an in the short gm so if you look here if we if you recall from the pipeline lesson with type name we have all the properties and methods and in that case there's a property status so that's how i'm accessing it dollar sign underscore dot status gives me actually test b go and look at this specific property okay so now obviously that was one part of it the other part of it was i did eq running so there's few others there's quite a few others and just to give you idea what is there we have eq or we have an e that starts not equals so what actually give me everything was not running so you see i have everything is stopped in that case there is only two stars really running stopped actually there's first type which you might see sometimes it's stopping with in this they did meet with but pretty much everything was not running not equal to running is being stopped uh what else we have we have dt that stands for greater so that's when you're working with dates or numbers lt less than le less than on equals ge greater than equals and we have also like like is actually taking white cards so when i have a cyrani and i just do ah say asterisk it will give me all the services that do have status of running or if i let's say we'll put a i focus on the name now or let's say the display name yeah so i will focus on that byte display name so it replace status with display name like i don't know w gives me it gives me some wizard i will filter it here and if you like to find a find out more about all other operators there are two about fives one is about operators so that's where you can find also mathematic ones but also the ones i told you about and many others like in not in contains operators so depending on your situation you're on and what you're trying to achieve one of them will definitely come handy oh there's one more more specific one if we do help about comparison [Music] ah about compression operators so that's the ones you can if you like to read more about that actually remind yourself how to use them okay so that was where object having filter script and you can pretty much use it whenever you you have an object and you can pipe that object into other ones so it's universal that's what you will be using very often it's kind of strongly encouraged to use built-in filters if they are in a function but sometimes you need to learn how to use how to use the syntax of them on the on the other hand you can just use way object and it will just work universally for every command list that you can you have objects to the only drawback is and i don't think there will be problem at the moment on an issue that y object will be probably a little bit slower than actually using native filter commands because actually when you use filter then all the filtering happens on the actual column address itself or database of a filtering film why why object needs to first of all retrieved to achieve all this data with this one and then all the data is being passed locally so when you do it say this on the remote server on the internet server that might just add to the time so that's the drawback and there's actually very nice alias to it rather than doing web object we can just use where and then we do curly brackets that gives me the same so it's even faster to use even when you are i don't know in the shell and you need that speed and should we do one more example let's maybe put in say get process and i will go and for example pick that so that would be where process name like i don't know everything that starts with say c there we go so that as you see you can just go and use it okay but for example because i can obviously go get savings and i have all this information but what if rather than having all these free fields i'm really after the status and for example display i don't care about the actual name as it is in file i just want this nice display name and status can i use shell to actually filter it out absolutely if i will go and pick let's say this and now i'll use another command called select object and i will give it what proposition after so i'm after display name and after those titles see it already stripped off what was unnecessary and gave me the status so really powerful very nice what if if we call for other lessons that obviously there might be more properties hidden from here for example when i do get process let's just focus on the actual there is one two three four like a six seven properties but actually there's much more if we do come back to our other command line get member if i go go get process and i will get member gm i can see like this look all of these are properties but they are hidden from me and i don't really know what they stand for what do they do or maybe i just want to actually access them because i am for example looking for the path where the application is running from can i rather than reading that i can do something like that get select object and white card so give me all properties you have that oh you see that's quite a lot of stuff going on there let's let's let it finish or if i just cancel it with ctrl c because i'm already after one so you see each object will be will have a lot of properties and it's separated by spacebar so that's how we can find it so this is get process actually what's there and there's much money for let's maybe let's maybe find one i don't know this one we see have name id and those how much cpu it used path yeah actually where it is where it's running from by default it was hidden from me however it's still there and it's still accessible and now once i know that i can actually access specific properties why not speak say a why not pick just these two purposes so say the name and the path let's try this so let's clear let's say this and now i can do select object or an august for that simply select and now that was name and that was path wasn't it that wasn't a lot of questions about select object when you thirdly need to select you an object a property that doesn't exist like i i need to actually once check once again what uh what was the name of that property it will create a table for you a column for you but it would be blank so when you see blank table it doesn't mean there's nothing you might be actually just asking for something that doesn't exist if that makes sense so let's try once again get service and once again what was the name okay ctrl c to just break and find ah we'll get service good process okay good process and if you see now me here doing mistakes yes this is normal this is absolutely usual that people do mistakes so if you work with shell and you do mistakes well i've been using this for years now and i still make mistakes as long as you don't make mistake on production and take down you know whole network down you'll be fine so if process name but where is path ah so it's path okay so let's try once again get process select nine half ah there you go much nicer so see i have the process name and path right on my hands so quite a few times at least when i was working at i had this doji application i wasn't really sure whether it was legit or was it dodgy because i just seen a name of it in task manager having this i can very easily find it and actually find why it's running from like we see powershell is running from here so one one more powerful tool and just to because for example now we did a little bit of where object and then we stripped off the data can i for example go use where object and then actually just save specific properties so let's say i'd like let's say let's find good service and i like them that all services that are stopped so that was status eco stopped okay let's see if that works oh i missed something ah i've missed the quotation mark see it's even highlighting the whole the whole line here but actually i've missed the the quotation mark so let's try once again good and now close the bracket okay i have only stopped once and for example i want all that start with let's say d because why not oh yes i absolutely forgot enough of that we can link multiple multiple uh statements by using either hyphen and or hyphen or and what is a difference in end both properties both statements must be valid for this to be true and with all if only one of them will be true you will get along and again so now accessing coming back another property and that was name like or maybe that ends with say y okay so if you see what i did here so name like hasta is quite so everything what will uh end with y do we have any like that oh there's a actually two and now i would only want the display name yeah so i want to find all services that are stopped that last letter is y and i like to only know the name so if i do select display name there we go so it's sometimes you need to send some information to the other person and they only really need that or can you please make sure that these services are there on that server and actually are there and i started because i noticed they are stopped something like that that's why it comes handy okay that was quite a lot of knowledge we really discovered couple of not two more common lots but they give you much more power and allow you to be much more selective in what you're actually managing and also understanding how objects works and how you can access their properties it's something what you'll be doing much more often work with shell but nevertheless let's summary so obviously we started with y object or it's alias where and that object as we know now allows us to filter it out and whatever we have in the pipe based on our base on the base on the query so it allows us to filter out information we actually want we've looked at the comparison operators and i showed you the how to find the help so actually that's how you can tell shell what exactly you're looking for what you want to study i want all services that are stopped or that are started all processes that start with c or been running on pc for x amount of time that's all we can find we also learned that current object symbol is dollar sign underscore so this is special sign and that pretty much tells whatever is in pipeline now we also learned how to learn property of the object by using dot so when i have a object like hit this one and i do dot followed by the property name i will be able to actually only access what's stored in that property and then we did select object so we could actually select specific objects we wanted to specific properties or we could also use it accessing all the properties that are in the object and yep remember to always filter left therefore what's what's is that about the the if you are like on that example you're bringing a lot of information from other server you want to filter it to the left so to the left of your of your pipe as much as you can because the sooner you filter out stuff the sooner your shell has less to do therefore you will work faster it will work better and there will be less overhead it probably won't be an issue for a long time but just keeping in mind if you can select things or you can filter them out early on the shell or even maybe in the first comment try to do that and coming up next i will drop you some exercises on the block i will also include the commands i've been using on that video so you can have a look if you like to remind yourself and on the next one we're actually now looking how to format and salt output because we've been actually not doing things but sometimes you won't actually nicely present stuff on the shelf therefore that's what will come up on the next video stay tuned have a nice evening and bye bye
Info
Channel: Kamil Pro
Views: 5,194
Rating: undefined out of 5
Keywords: powershell, course, it, sysadmin, 2020, where, where-object, select, select-object, filter
Id: oxSGpoL1lQg
Channel Id: undefined
Length: 23min 10sec (1390 seconds)
Published: Fri Aug 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.