- Windows CMD: PATH Variable – Add To PATH – Echo PATH
- Echo Windows PATH Variable
- Add To Windows PATH
- Set Windows PATH For The Current Session
- Set Windows PATH Permanently
- Test-Path
- Syntax
- Description
- Examples
- Example 1: Test a path
- Example 2: Test the path of a profile
- Example 3: Check whether there are any files besides a specified type
- Example 4: Check for a file
- Example 5: Check paths in the Registry
- Example 6: Test if a file is newer than a specified date
- Example 7: Test a path with null as the value
- Example 8: Test a path with whitespace as the value
- Parameters
- Inputs
- Outputs
- Notes
- How to check your path in Command Prompt
- 2 Answers 2
- How to get the path of the batch script in Windows?
- 8 Answers 8
Windows CMD: PATH Variable – Add To PATH – Echo PATH
PATH is an environment variable that specifies a set of directories, separated with semicolons ( ; ), where executable programs are located.
In this note i am showing how to print the contents of Windows PATH environment variable from the Windows command prompt.
I am also showing how to add a directory to Windows PATH permanently or for the current session only.
Cool Tip: List environment variables in Windows! Read More →
Echo Windows PATH Variable
Print the contents of the Windows PATH variable from cmd :
The above commands return all directories in Windows PATH environment variable on a single line separated with semicolons ( ; ) that is not very readable.
To print each entry of Windows PATH variable on a new line, execute:
Cool Tip: Set environment variables in Windows! Read More →
Add To Windows PATH
Warning! This solution may be destructive as Windows truncates PATH to 1024 characters. Make a backup of PATH before any modifications.
Save the contents of the Windows PATH environment variable to C:\path-backup.txt file:
Set Windows PATH For The Current Session
Set Windows PATH variable for the current session:
Set Windows PATH Permanently
Run as Administrator: The setx command is only available starting from Windows 7 and requires elevated command prompt.
Permanently add a directory to the user PATH variable:
Permanently add a directory to the system PATH variable (for all users):
Info: To see the changes after running setx – open a new command prompt.
Test-Path
Determines whether all elements of a path exist.
Syntax
Description
The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element. If the Path is whitespace an empty string, then $False is returned. If the Path is $null , array of $null or empty array, a non-terminating error is returned.
Examples
Example 1: Test a path
This command checks whether all elements in the path exist, that is, the C: directory, the Documents and Settings directory, and the DavidC directory. If any are missing, the cmdlet returns $False . Otherwise, it returns $True .
Example 2: Test the path of a profile
These commands test the path of the PowerShell profile.
The first command determines whether all elements in the path exist. The second command determines whether the syntax of the path is correct. In this case, the path is $False , but the syntax is correct $True . These commands use $profile , the automatic variable that points to the location for the profile, even if the profile does not exist.
For more information about automatic variables, see about_Automatic_Variables.
Example 3: Check whether there are any files besides a specified type
This command checks whether there are any files in the Commercial Buildings directory other than .dwg files.
The command uses the Path parameter to specify the path. Because the path includes a space, the path is enclosed in quotation marks. The asterisk at the end of the path indicates the contents of the Commercial Building directory. With long paths, such as this one, type the first few letters of the path, and then use the TAB key to complete the path.
The command specifies the Exclude parameter to specify files that will be omitted from the evaluation.
In this case, because the directory contains only .dwg files, the result is $False .
Example 4: Check for a file
This command checks whether the path stored in the $profile variable leads to a file. In this case, because the PowerShell profile is a .ps1 file, the cmdlet returns $True .
Example 5: Check paths in the Registry
These commands use Test-Path with the PowerShell registry provider.
The first command tests whether the registry path of the Microsoft.PowerShell registry key is correct on the system. If PowerShell is installed correctly, the cmdlet returns $True .
Test-Path does not work correctly with all PowerShell providers. For example, you can use Test-Path to test the path of a registry key, but if you use it to test the path of a registry entry, it always returns $False , even if the registry entry is present.
Example 6: Test if a file is newer than a specified date
This command uses the NewerThan dynamic parameter to determine whether the «PowerShell.exe» file on the computer is newer than «July 13, 2009».
The NewerThan parameter works only in file system drives.
Example 7: Test a path with null as the value
The error returned for null , array of null or empty array is a non-terminating error. It can be suppress by using -ErrorAction SilentlyContinue . The following example shows all cases which return the NullPathNotPermitted error.
Example 8: Test a path with whitespace as the value
When a whitespace or empty string is provided for the the -Path parameter, it returns False. The following example show whitespace and empty string.
Parameters
This parameter is not supported by any providers installed with PowerShell. To impersonate another user, or elevate your credentials when running this cmdlet, use Invoke-Command.
Type: | PSCredential |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies items that this cmdlet omits. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as «*.txt». Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies a filter in the format or language of the provider. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcard characters, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when it retrieves the objects instead of having PowerShell filter the objects after they are retrieved.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies paths that this cmdlet tests. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as «*.txt». Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist. This cmdlet returns $True if the path syntax is valid and $False if it is not.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a path to be tested. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If the path includes characters that could be interpreted by PowerShell as escape sequences, you must enclose the path in single quote so that they won’t be interpreted.
Type: | String [ ] |
Aliases: | PSPath, LP |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specify a time as a DateTime object.
Type: | Nullable [ DateTime ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specify a time as a DateTime object.
Type: | Nullable [ DateTime ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a path to be tested. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.
Type: | String [ ] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | True |
Specifies the type of the final element in the path. This cmdlet returns $True if the element is of the specified type and $False if it is not. The acceptable values for this parameter are:
- Container. An element that contains other elements, such as a directory or registry key.
- Leaf. An element that does not contain other elements, such as a file.
- Any. Either a container or a leaf.
Tells whether the final element in the path is of a particular type.
Up to PowerShell version 6.1.2, when the IsValid and PathType switches are specified together, the Test-Path cmdlet ignores the PathType switch and only validates the syntactic path without validating the path type.
According to issue #8607, fixing this behavior may be a breaking change in a future version, where the IsValid and PathType switches belong to separate parameter sets, and thus, cannot be used together avoiding this confusion.
Type: | TestPathType |
Aliases: | Type |
Accepted values: | Any, Container, Leaf |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe a string that contains a path, but not a literal path, to this cmdlet.
Outputs
The cmdlet returns a Boolean value.
Notes
The cmdlets that contain the Path noun (the Path cmdlets) work with path names and return the names in a concise format that all PowerShell providers can interpret. They are designed for use in programs and scripts where you want to display all or part of a path name in a particular format. Use them as you would use Dirname, Normpath, Realpath, Join, or other path manipulators.
The Test-Path is designed to work with the data exposed by any provider. To list the providers available in your session, type Get-PSProvider . For more information, see about_Providers.
How to check your path in Command Prompt
I’m learning Laravel and found this direction on their documentation:
«Make sure to place the
/.composer/vendor/bin directory in your PATH so the laravel executable is found when you run the laravel command in your terminal.» http://laravel.com/docs/4.2/installation#install-laravel
How exactly I make sure the folder is on my PATH? What’s the command?
If I’m not on the PATH, how to get there? I tried to find /.composer/vendor/bin but got nothing.
I’m using Windows 8 64bit.
2 Answers 2
1) windows: PATH | grep -oP «.composer.*bin»
If it’s in the path You’ll get a response if it’s not you’ll get nothing.
/ refers to home path on linux doing %HOMEPATH% on windows results in the same thing, which in your case would be %HOMEPATH%/.composer/vendor/bin
Doing /somefolder Will go to the root path in linux and try to find a folder there called somefolder but won’t do anything in windows, I’m not sure what the equivalent in windows is, might be referring to root of the drive, I.e. C:\ or D:\ etc.
To add .composer/vendor/bin to your home path you need to go to your environmental variables and add it into the path you can go here and follow the steps in the answer or the question.
The important bit:
Example of windows SETX command:
Print the PATH environment variable:
C:\Users\Charity>echo %PATH% C:\windows\system32;C:\windows and space;C:\foobar Use setx to set the environment variable:
C:\Users\Charity>setx PATH «%PATH%;C:\zombiepoke» SUCCESS: Specified value was saved. Close and re-open cmd terminal, then run:
C:\Users\Charity>echo %PATH% C:\windows\system32;C:\windows and space;C:\foobar;C:\zombiepoke You have to be careful with double quotes. If you let quotes get into your path variable it might break something. However they are necessary for specifying addendums to the original %PATH%.
Make note though that this only sets it for the current user context, to set it for all users you have to to use setx /M .
How to get the path of the batch script in Windows?
I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.bat
I would path to be equal to c:\path\to\my\file
How could I achieve that ?
dpf0″ would be more reliable for this case. – eckes Jan 14 ’17 at 17:59
8 Answers 8
To remove the final backslash, you can use the :n,m substring syntax, like so:
I don’t believe there’s a way to combine the %0 syntax with the :
n,m syntax, unfortunately.
0\.. — knew there had to be a better way! Also, you will probably want to enclose %
dp0 in double quotation marks ( «» ) in case there’s spaces in the directory name, etc. – Cameron Sep 30 ’10 at 3:56
dp0 contains the « at the end. Do you have an idea how to remove it ? – Misha Moroshko Sep 30 ’10 at 3:56
0,-1$ in it. Still—very nice answer. – Kyle Strand Sep 21 ’16 at 5:04
dp0 may be a relative path. To convert it to a full path, try something like this:
dp0 directly? – jpaugh Mar 28 ’16 at 19:04
dp0 can be relative, which may or may not be a problem depending on use case – Michael Mrozek Feb 6 ’17 at 19:52
dp0 can’t contain a relative path, d stands for drive and p for path, how a drive could be relative? – jeb Mar 31 ’17 at 15:30
dp0 will be an absolute path even when the script was run as a relative path. Thanks to jeb’s comment, I was not fooled by this answer. Why do people just make up stuff and go and start spreading their wild imagination to others. I have this colleague who does this, but I blamed his (young) age. I wish my down-vote would count. – bitoolean May 25 ’18 at 14:25