Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I tell if a specified folder is in my PATH using PowerShell?

A function like this would be great:

function FolderIsInPATH($Path_to_directory) {
    # If the directory is in PATH, return true, otherwise false
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can get your PATH using [Environment]::GetEnvironmentVariables()

[Environment]::GetEnvironmentVariables()

Or if you want to get the user environment variables:

[Environment]::GetEnvironmentVariables("User")

Next get the PATH variable:

$Path = [Environment]::GetEnvironmentVariables().Path # returns the PATH

Then check if the specified folder is in your PATH:

$Path.Contains($Path_to_directory + ";")

The function put together:

function FolderIsInPath($Path_to_directory) {
    return [Environment]::GetEnvironmentVariables("User").Path.Contains($Path_to_directory + ";")
}

However, this function is case-sensitive. You can use String.ToLower() to make it not case-sensitive.

function FolderIsInPath($Path_to_directory) {
    return [Environment]::GetEnvironmentVariables("User").Path.ToLower().Contains($Path_to_directory.ToLower() + ";")
}

Now call your function like this:

FolderIsInPath("C:\\path\\to\\directory")

Note that the path must be absolute.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading