PowerShell Quick Tips : Test-Path

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip video in this video we're going to be taking a look at the commandlet of test path we've seen this commandlet in quite a few videos if you've been following the channel for quite a while uh but the test path can actually do quite a bit more than what we've actually seen in our tutorial videos so we're going to take a little Deep dive in to see exactly what we can do with the test path commandlet so let's actually go ahead and let's get started here so what I did actually do before the video started here is I actually do have two folders and these folders have some files in it so the first folder called test folder has a test.log any test Dot txt and the test folder to just simply has a test dot txt so these are going to be the files and folders that we're going to be testing the paths with in our last example we're going to be actually testing a registry path I'm going to show you guys the limitations of the test path on the registry but also what it is capable of doing so let's actually go ahead and show off the first example here which is the most basic example that we've seen in a lot of our videos which is just simply seeing if a path actually exists so what we're just going to do is we're going to put in our commandlet of test Dash path we're going to put in a dash path parameter and we are actually just going to pass in the path here of our test.txt file in our test folder folder here and if we run this we get true and if we go ahead and we actually copy this line underneath and we just add a 1 here which does not actually exist we will actually get the value of false so this is extremely useful especially if you have scripts that kind of manipulate files you could easily just then go if test path and then just put the path to the file and then put a set of curly brackets and you could do right outputs uh exists and then you can put an else here and then we can just copy this line over and say does not exist all right so if we actually run this here it will go ahead and test the path and give us the proper entry here and if we just change it to a path that does not actually exist we get does not exist so this is very useful if you're reading in content in a script and you want to make sure that the file actually exists uh test path is extremely handy and just lets you do that check instead of getting errors instead of doing like a try catch to catch that error it is often the best case scenario just make sure that path exists and then do your actions so what you can actually do as well with the test path is if we take our example here and we just paste it on a new line we can actually test to see if the end point that we're the path that we've put in is a folder or a file so we can actually add a parameter of path type and we get container or leaf or any in this case um container is a folder and a leaf is a file or just like an endpoint uh similar idea a file executable text file log file CSV file anything of that point that does not contain anything and a container is a folder so it contains other objects inside of it so let's go ahead and let's test to see if our test.txt file are our path for test.txt is a container now we will actually get the answer of false here which is what we expected and then if we actually go ahead and we just copy this line and we test it to see if it is a leaf and we just run this line here we will get true so now if we actually just change this line here to just see if it's just checking the test folder it's checking to see if it's a container we will get the value of true and then of course if we copy this line down here and we check to see if the test folder is a leaf or if it's a file we will actually get back false because it is just a container it is a file so these are all things again that you could use in your if statements to make sure like that your folder path exists to make sure that your specific files exist to make sure that it just it is the proper object the proper type that you're expecting to deal with you can add all of these checks into your code with test path now something else that you can actually do with test path is you can actually test to see if specific files exist in a folder but also you can check to see if it contains anything other than specific items so what we can actually do here let's see the option of let's make sure that something is in there so we can do a test path we could put in once again our path here and we're going to put in the path to our folder so we're going to put the path in to our test folder and we are going to put a include and in our include here we are going to do a star DOT log now this will actually come back as being of course uh it should come back as true and I forgot something when you do this you do have to add a slash star at the end so it will get all the items inside of test folder and then we will get the value of true because it does actually contain a DOT log file now if we were going to do the same thing here so copy paste this line but change it from test folder to test folder here test folder two where we actually do not have a test DOT log we actually run this to see if it includes any DOT log files and we get false so that is something very um good to check what we can even do is you can remove this wild card here and we can test for just test two DOT log in our test folder and we will see that this comes back as false but if we check for test.log we will get this as true so maybe you're testing something in your code you're going through some directories to see if certain log files got created and you know the name that you're expecting or maybe it's a process that generates a specific file with a specific name you can go ahead and create a script to just make sure that gets generated every day and make sure that it actually exists in that path now similarly as it has and includes we can actually do a exclude test as well so that will make sure that we'll just test to see if there's any files other than the file that you put in there so we can do another test path here we're going to put in the test folder again and we are going to say we are going to exclude and we're going to put in a string here we're going to do a star DOT log now in the test folder we have a DOT log file any dot text file so this will actually equivalate to true because it has something other than a log file so now if we just copy this line and we do the same folder with DOT txt we will of course still get true because it does have a DOT log file but now if we actually do this test on test folder 2 here so let's do the same tests the same two tests on test folder two so let's see if it's test path excludes DOT log it's a true and that's because it doesn't have a DOT log file in test folder two but it does have a DOT txt so it does actually match the conditions but if we exclude the dot txt on the test folder 2 we will get false because the only type of file that exists in test folder 2 is a DOT txt file now once again you can remove these wildcards if we test for test two dot text we will get true because that file does not exist but test dot txt actually exists now the exclude options may be a little bit more obscure for certain examples um that you would want to do this I would actually kind of similarly do the same test you can use the same tests as you did for include maybe you want to make sure that something doesn't get generated by a specific application so you can test if it excludes that specific name if it doesn't exclude that specific name that you know that file exists in which case maybe you can go and open it and see what happens it's a little bit more obscure it's kind of working like the negative of passion but it is there for you guys to actually use now the neat little feature the neat little parameter that you can actually use on test path as well is newer than or older than and that will actually see when the last right time of the file was to see if it's newer or older than a certain date so what we can actually do here is let's take our test.txt file in our test folder and let's go ahead and let's say if it is newer then so you're going to see it doesn't actually auto complete for me for some reason but if you just do test Dash sometimes you will see it here but when you have it in after a path sometimes you will not actually see it come in but do be aware that it is actually an option so if we do newer than like this and this will actually take a string or a date time object I prefer to use date time objects it's just a lot easier to make sure that the formatting is correct so we're going to do a get date dot add days and we're going to say minus one so is this file newer than yesterday and we are going to get true so I can actually take this same half here and if I just change newer then to older then if it's older than yesterday we get false now all these files were created today so that is the example there so you can easily see if a file has been written recently or not written recently and get that check as well so maybe you want to check to make sure that a specific file is newer than a certain date so what we can actually do is let's do this here so let's check all the files in test folder and we want to make sure that we include the DOT log file so we can actually say in this one we are testing for the files for DOT logs in test folder to make sure that they are newer than yesterday And if we actually go ahead and run this test we will see that it is true the log file is newer than yesterday and if we run that same thing but then do an older then we will see that it is false and if we want to check to see if test2.log like this two DOT log exists and newer and older we will see that both of these equivalate to false so you can use these in combination you can even add to make sure that it is a leaf item a container item that is newer or older than as well so those those are all possible and now the final option for test path is actually testing registry paths now one thing to note about testing registry paths is I'm going to open up my registry here and show you guys what we're going to be testing so we're going to be testing the current user software Microsoft Edge this way it's just something that I know that everybody will have and we are going to make sure that this folder exists and then we're going to see if this key exists now that's going to be a very good thing to remember and we are going to see what happens here so what I like to do is I like to just copy this path here we're gonna have to do a little bit of manipulation to it to get it to work properly so we're going to do a test Dash path now we've seen this a little bit in some videos on how to reference the registry in Powershell but if you guys don't know we are going to be doing that right now so we're going to be testing for a path now for H Key current user what that will actually be is h k c u colon backslash and now as you can see we already get some options here so we're going to do backslash software backslash Microsoft backslash Edge we can actually go ahead we can delete this here if we test this we do get true we know that that folder exists in the registry now let's go see if the if we can go ahead and find the entry here so that entry was called usage stats usage stats in Sample so let's do usage usage stats in Sample and if we go ahead and we run this we will get false but we know that it actually exists now this is the limitation of the test path test path can only make sure that the registry directory or object exists but it cannot check any entries or keys inside of it so we would be able to check if the extensions exists so if we go ahead and we test path here and we go backslash extensions we will see that that will work but none of the information will actually show up so if we were to go inside default we would not be able to see that is DSE recommended through the test path there are other commandlets that we have seen that does actually show how to do that but it is not an option in the test path registry so you can see the actual containers just not the registry entries and that is pretty much it for the test path commandlet now the test path commandlet most people we use it just very very simply but it is capable of doing much more powerful checks than just checking if a path simply exists hopefully this helps you guys maybe you guys can use this in your scripts in production or in your personal lab environments if you guys have any comments or questions please let me know in the comment section down below and if you guys have any Camillus that you guys would like me to Deep dive on please let me know in the comment section as well please be sure to hit that subscribe button that like button and be sure to hit that notification Bell to be notified when that next video comes out I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 1,325
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, test-path, powershell test-path
Id: 9eKe51ovlw8
Channel Id: undefined
Length: 16min 21sec (981 seconds)
Published: Mon Jun 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.