PowerShell Quick Tips : Get-ChildItem

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip video in this quick tip video we're going to be taking a look at the get child item commandlet so this commandlet is often used to kind of look through folders and files you could parse through and recurse through all these different files and folders which we're going to be seeing how to do that today as well but something that's often not known about get child item is you can also use it on the registry and on the certificate store as well so we'll be looking at those examples at the end of the video as well so let's actually go ahead and let's get started at looking at what exactly does get child item do so we're going to be using um the C scripts directory on my computer it has a lot of different folders it does have a hidden file here so if I actually go ahead in the properties here we will actually see that it is a hidden file so we will actually be able to look at those and see the different options that we have with get child item so let's just go ahead and let's do a get child item with a path here and we're going to do a c colon backslash Scripts and we are going to go ahead and we are going to run this and this is going to give us just the exact layout of our C Scripts which actually did not there you go so here is all the different files and folders in our C scripts here so if we actually just open this up and go into scripts we will actually see here we have 2 through 11 and then intermediate tutorials quick tips and the test hidden so we do have two um through till 11 our intermediate tutorials and quick tips but we don't have that hidden file so there is actually two methods of getting hidden files with get child item we can actually add the parameter called hidden but when we actually execute this one we actually only get the hidden files so the parameter of hidden will only get you hidden files so only gets hidden files what we can actually do is add a another parameter called Dash force and this will actually forcefully get all of the files and folders including the hidden ones so here now we have our 2 through 11 folders intermediate tutorials quick tips and our test hidden Dot txt so this is great we are able to get our hidden files but now let's say we actually wanted to go ahead and we wanted to parse through all these different folders what we can actually do with get child item actually let me just add a comment here gets all files including hidden file all right what we can actually do is add R so we're going to keep the force on here and what we can actually do is add recurse and what this is going to do is going to get us all the files and folders and actually go inside the folders and get us the files and folders in there and keep going till it is only files so if we actually execute this we will actually see we get a bunch of different options here uh so we have a bunch of the different directories the files inside of those directories it just creates us a whole list of stuff here so this could be useful if we want to get all the files and folders including all the child folders and files inside of all the child folders and files it'll just keep going all the way to the end maybe we want to do this maybe we want to get all the files on the C drive and we just want to know all the name of all the different files because we want to store the harsh of those to make sure that none of them change that is just a purely an example here so it gets all files and folders and recurses into child folders but now let's say if we wanted to go ahead and just take our original option here which was just get child item force and let's say we only wanted to go into these folders one level down we didn't want to go all the way down we only wanted to go one level you can actually specify that using the depth parameter and in here you would actually be able to specify a number so if we do one we will only go one folder in now if we do two we're going to go two folders in so as you can see this you can quickly easily get back to recurse if you know exactly how many folders are in there or if you just put like an absurdly large number in here and if you just put zero what will happen is it will just get you the default option where it just gets the child item it will not go into a folder so once you start at one it will go into the folders once once you put two it'll go into those folders and those folders child folders as well and so on if you go to three four and Beyond uh so let's just do that here get all files and folders and one level down as well and then we can also apply some different filters and some different types of filters like includes or exclusions of files that we want or don't want on these as well so let's actually just go ahead and once again let's just do a force and a recurse here because we want to go through all of them and what we're going to do is we're going to do a filter and all we want to do is actually filter on a star dot PS1 so this will actually give us all of the Powershell Scripts so if we actually run this it goes through all the different folders and will actually only give us back the dot PS1 files which are which is our Powershell scripts now if I change this filter I can change this to an include and it will give me the exact same results as well and what I could even do is let's say I wanted to um I wanted to eliminate um all the files that start with just script.ps1 what I can actually do is do an exclude here and I could do a script so anything that starts with script I don't want it and there it is now I should not have that script file and it does not come up um so that is an example there that you can use exclude if you do put include or exclude it doesn't matter what order you put them in the exclusions will always interact afterwards so if you include something but it's actually in the exclusion as well the exclusion will always remove it from the result set even if you put the exclude before the include in your parameter list so that is something to remember but even what you can even do is let's say we wanted to instead of getting all the Powershell scripts let's get everything that is not a Powershell script we would also be able to do that so now it only gives us our text files our CSV Json XML files our folders um now as you can see as I just mentioned it gets us the folders as well let's say we only wanted our files what we can actually do is also add a parameter called file here and this will only return us files it will not return us any directories so here we'll just give us the file names no directories and of course there is also a parameter to just return us the directories if all we wanted is just the folder names and we didn't actually care about the file names we can actually do that completely as well so that could be very very handy if all you need is folder names instead of file names that pretty much covers it for the files and folders with get child item now let's actually take a look at some of the more obscure things that you can get with get child item that people might not necessarily know but that could be very useful now let's say you wanted to check some different settings on a bunch of your different servers so you can use this with Powershell remoting of course and you wanted to check a specific registry setting because there should be a specific setting in the registry for a certain option what we can actually do is if you do a get child item and then a path here and then in the path what we're going to do is we're going to do a set of double quotes and we're just going to do an h k l m and this is going to be for the hkey local machine now if you want to know where I got this from if you open up your reg edit on your computer we will actually see the hkey local machine hkey current user H key classes root so this is the h k l m this would be hkcu this is H key u h k c c and hkcr so that might be a little tricky at first but once you kind of get used to them and most of the time in my experience anyways I've usually always used either current user or local machine most of the time it is the local machine so let's take a look at local machine and we're actually going to be going into system current control set policies hardware and Bluetooth now Bluetooth actually has properties so this is actually it looks like a folder but according to Windows when you're actually looking at the system Bluetooth actually becomes an item so we're gonna actually see how that kind of works out in just a second here so let's do hklm backslash now of course you're always going to get this auto complete when you're dealing with Powershell in Visual Studio code or even in the ISE depending our Visual Studio code mostly depending on if you have that extension properly set up and in the ISE you will have this as well so let's go into system Uh current control set policies Hardware and then here we have Bluetooth it will show up as a folder but once I actually get this here so let's do our get child item we will see that we actually get nothing and even if I add a slash after Bluetooth I still get nothing so you might think that it's not working properly but actually if you just bring it down to Hardware backslash and not put the Bluetooth in here and you just run this you will actually see we get the results that we want we actually get our our hardware and then we have all the different options and there is the name of our option here Bluetooth and then there's all the different properties uh which are right here so you can actually definitely get these if you wanted to get the specific Bluetooth item properties there is a separate commandlet for that if you wanted to specify the slash Bluetooth slash it would be get item property because Bluetooth is actually an item and not a actual folder but get child item you can actually manipulate the registry you would just have to make sure that you know the exact path so make sure that you test it out first maybe also create a pastor script to make sure that you're getting back the properties that you're actually expecting and then in here you would then be able to go ahead and check make sure that your certain options are set up like allow advertising is one allow Bluetooth is set to two now I don't actually know what these settings actually stand for or the numbers actually stand for I just picked Bluetooth as a random example here but you would be able to get child item on registry entries and make sure that the registry entries match what they should be and if they aren't maybe alert someone or you can then use different commandlets to actually interact with the registry and make that registry change now another thing that you can use get child item four is actually the certificate store now this could be again very useful for your different servers especially if you have a lot of certificates that you might be dealing with and you want to see where that certificate is being used uh what I've done in the past is I actually get all my servers from active directory and then I will run remote code on them and get the child item for the certificate store looking for the specific certificate common and friendly name and then it tells me if that server actually has that certificate on it or not so it tells me where when I actually renew that certificate on GoDaddy or digisert it tells me that where I need to replace it so this could be something very very useful maybe not super useful for your personal computers but maybe for your home Labs if you have SSL certificates or definitely for your work environment if you guys actually manage a lot of different web services or different services that require SSL certificates so to do that here is you would do a get Dash child item and then the path parameter and then you would put a cert colon backslash and then once again you're going to have that autocomplete here we're going to do local machine and we're just going to look at the root here and if we actually just we could also wrap this at Double quotes of course will not change anything and we get all of our certificates and as you can see we got some information we don't get all the information but if we actually pipe this to a select Dash object and then a star we will actually get all that information in here for us so we can actually get even the let me just find one here so we can get the not after so you can actually find expired certificates this is what I used so what I typically do for a certificate would be um as I would have a where Dash object here and then of course I would specify it was the friendly name first but I don't this is a personal computer so I don't actually have a certificate was a friendly name that I would know that would potentially be expiring but here we would have a where object not after and then lesser than and then what you do is you would put a variable wrapper here and do the get Dash date commandlet and then add days per se and add let's just add 365 days to make it a year I could have just done the ad years in this case but what you could do is if you're writing a script to know where their certificates expiring maybe you want to know 60 days ahead of time so you would put 60 and this way you'd be able to plan accordingly with the different people on your team to replace those certificates on time so they don't expire on that server but here we go if we actually run this right now on my local machine root of where not after is less than a year from now we will get tons and you should also probably get tons if you run this on your personal computer because if we actually output this to out grid view which we've seen on another quick tip video um we can actually see um so what we're just going to do is we're just going to make sure that we pipe that first to a select object star and that will give us all the different properties and then here we can actually take a look at it so here we have all the different paths all the different bump prints of the different certificates that are expiring within a year the friendly names of the certificates that are expiring within a year and here is the actual data here so actually all these are already expired you'll probably have these on your personal computer as well for some reason I'm not 100 sure why but it seems like Windows even Windows 11 still comes with very very old certificates just on there expiring in 1999 um in 2004 even so this could be a very useful tool again more for a professional environment if you're using SSL certificates filter it by the friendly name and then filter it by the not after and then give yourself a little buffer time because of course GoDaddy and digisert will tell you when the certa is expiring and give you enough time to renew it but after that if for some reason someone that's put in a system has quit or left since then and now that certificate is renewing and you're not sure what server it's on being able to run this on every server and seeing if that certificate is there or not is very very useful uh so that is another use for the get child item I would highly recommend checking out the get child item or the certificate store and the registry and not just file in folders although the file and folders are very useful uh you could probably get a lot more benefits out of get child item for your automation scripts with the registry checks and the certificate stores so hopefully that guys helped that helped you guys with the get child item commandlet if you guys have any other command lists that you guys would like me to go over please let me know in the comment section down below or if you guys have any comments or questions please let me know in the comment section down below as well I will try my best to get back to you guys or if it's something that could benefit a lot of people I will make a video on it be sure to hit that subscribe button hit that like button and hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 4,213
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, scripting, powershell scripting, powershell scripting tutorial, powershell tutorial, how to, powershell api responses, powershell automation, powershell beginners, powershell variables, quick tips powershell, quick tips, learn powershell, powershell for beginners, powershell commands for beginners, shortcut, get-childitem, powershell get-childitem, powershell registry, registry, powershell certificates, certificates
Id: P7USOGVGNQM
Channel Id: undefined
Length: 19min 46sec (1186 seconds)
Published: Thu Dec 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.