Test the existence of an element (file, folder, etc.)

I. Introduction

This chapter focuses on a basic yet indispensable cmdlet: "Test-Path"! This cmdlet is extremely useful in PowerShell, as it can be used to test the existence of an element, such as a file or folder.

This cmdlet is designed to return a Boolean, i.e. true or false. We'll see that it has several parameters that can be used to go further than a simple check.

II. PowerShell: testing the existence of a file or folder

To use "Test-Path", we specify the path of the folder or file to be tested after the command name. For example, to check the existence of the "hosts" file on machine :

Test-Path "C:WindowsSystem32driversetchosts"

If we want to use this command according to good practice, we must add the "-Path" parameter:

Test-Path -Path "C:WindowsSystem32driversetchosts"

The command should return "true", as this file is natively present on Windows.

Test-Path

The interesting thing is to use "Test-Path" in a conditional structure (in an "If-Else", for example), to perform an action only if the file exists. For example, we can retrieve the contents of the "hosts" file only if it exists:

$File = "C:\Windows\System32\drivers\etc\hosts"

If(Test-Path -Path $File)
{
Get-Content $File
}
Else
{
Write-Output "The file does not exist!"
}

As you can see, the if condition doesn't actually contain a condition. Since the "Test-Path" command directly returns a Boolean, the if condition will be able to interpret the value returned by "Test-Path". Thus, the code in the "If" block will only be executed if "Test-Path" returns "true".

If you wish to test the existence of a folder, simply specify the path to a folder, rather than the path to a file.

III. Testing the existence of an environment variable

In the same way, we can check the existence of an environment variable with "Test-Path". The syntax remains the same! For example, with the"USERPROFILE" variable:

Test-Path -Path env:USERPROFILE

In the same way, we can test for the existence of a path that targets the Windows Registry.

IV. Exploring Test-Path parameters

Like most PowerShell cmdlets, Test-Path has its own set of parameters. I'm going to show you some of these parameters by using examples.

  • Test-Path and -isValid

When we use the "Test-Path" cmdlet, it returns true or false depending on whether or not the element under test exists. If we use the "-isValid" parameter, the behavior is different: we don't check whether the element exists, we check the consistency of the value to be tested.

The "C:IT-CONNECT" directory doesn't exist, but the syntax is correct, so the command will return "true" if "-isValid" is specified.

Test-Path -Path "C:IT-CONNECT" -IsValid

The proof is in the pictures below:

  • Test-Path and -NewerThan and/or -OlderThan

We can test the existence of a folder and its last modification date, to see if it is"newer than"(NewerThan) or"older than"(OlderThan) by specifying a date. We can combine the two parameters to target a specific period.

Let's take the "hosts" file, which on my machine has a modification date of 02/01/2024. We can test the date in this way, using the classic"day/month/year" format or another compliant format:

Test-Path -Path "C:WindowsSystem32driversetchosts" -NewerThan "01/01/2024"

Or by combining the two parameters:

Test-Path -Path "C:WindowsSystem32driversetchosts" -NewerThan "01/01/2024" -OlderThan "30/04/2024"

Here's another example, based on other dates, in pictures:

  • Test-Path and -Exclude

We can use "Test-Path" to look at the contents of a folder, and in particular to see if there are any files in an undesirable format in a folder.

If we take the example of the"C:TEMPDOSSIER1" folder, which contains only JPG and PNG images, we can ensure that it doesn't contain elements in other formats.

We'll use"-Exclude" followed by our JPG and PNG extensions to exclude them since they're legitimate.

Test-Path -Path "C:TEMPDOSSIER1*.*" -Exclude *.jpg, *.png

If the folder contains only JPG and PNG files, the result will be"false": in this case, you can be sure that the folder contains no files in other formats! Note the existence of the"-Include" parameter, which works in the opposite way. It can be used to ensure that there are files in one or more formats.

V. Conclusion

The "Test-Path" cmdlet is generally used in its simplest form, i.e. to test the existence of a file or folder. However, as we've just seen, it has a number of very interesting parameters for carrying out a wide variety of tests, with the advantage of always returning a Boolean.