PowerShell: Path Tutorial
Path parameter
Many commands have a -Path
parameter.
Path
parameter is a positional parameter and named parameter. That means, it can be first arg, or with -path value
anywhere.
# list file of a given path dir c:/Users/xah/web/ # or dir -Path c:/Users/xah/web/
Path Value Are Expanded
Path
value can contain abbrev, such as ~/
to mean $home
, and String Wildcards such as *
for any characters.
dir ~/web/ # same as dir c:/Users/xah/web/
Use -LiteralPath
if you do not want
Path Expansion.
Default Path Value
If Path
parameter is omitted, default value is current dir.
dir # same as dir .
Relative Path, Full Path
The -Path
parameter accepts both full path or relative path.
cd dir Documents # same as dir c:/Users/xah/Documents/
Path Value is String Type, Quote Path that Contains Space
If the path contains space, it needs to be quoted.
dir "c:/Users/xah/Saved Games"
[see PowerShell: String]
Note: quoted path is also expanded. [see PowerShell: Path Expansion]
Path separator
Path separator is the backslash, but slash is also supported. They can be mixed.
All the following works the same:
dir c:/Users/xah/web/
dir c:\Users\xah\web\
dir c:\Users/xah\web\
single dot, double dots
- Single dot
.
means the current dir. - Double dot
..
means the parent dir.
For example:
cd ..
means go up one dircd ../..
means go up two dirs
current dir
Get-Location
(alias gl
, pwd
)
return the current dir.
Path expansion
LiteralPath
The -LiteralPath
parameter lets you input a path without expansion.
# list children of a dir named math* as is dir -LiteralPath "math*"