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 files of a given path dir "c:/Program Files/" # or dir -Path "c:/Program Files/"
Path Value is String Type
- Path value is String type.
- Normally, quote can be omitted, but if path contains space, it must be quoted.
- Double quoted string goes thru String Expansion.
- To prevent string expansion, use single quote. (but Path Expansion is still done. Use parameter LiteralPath to prevent that.)
# use quote when path contains space dir "c:/Program Files/"
# path value of double quoted string # or # no quote # goes thru string expansion # watch out for dollar sign in it $xx = " Files" dir c:/Program$xx/ # same as dir "c:/Program Files/"
Path Expansion
Path
value can contain abbrev, such as beginning ~
to mean $home
, and String Wildcards such as *
for any characters.
Path value is expanded by path expansion. (even for single quoted path.)
Default Path Value
If Path
parameter is omitted, default value is current dir (except the cd
command, which default to $home
).
dir # same as dir . # same as dir (Get-Location)
Relative Path, Full Path
The -Path
parameter accepts both full path or relative path.
cd $home dir "Documents" # same as dir "$home/Documents/"
Path separator
Path separator is the backslash, but slash is also supported. They can be mixed.
All the following works the same:
"c:/Program Files/Windows Defender/"
"c:\Program Files\Windows Defender\"
"c:/Program Files\Windows Defender/"
single dot, double dots
- Single dot
.
means the current dir. - Double dot
..
means the parent dir.
# go up one dir cd .. # go up two dirs cd ../..
show current dir
Get-Location
(alias gl
, pwd
)
return the current dir.
PowerShell Path
PowerShell, List Dirs and Files
List Dirs
- PowerShell: Navigate Directory
- PowerShell: Show Current Dir Path
- PowerShell: List Directories
- Show Directory as Tree
- PowerShell: List Empty Dir 🚀
- PowerShell: Dir Size 🚀
List Files
- PowerShell: List Files
- PowerShell: Show Fullpath, No Truncate Lines
- PowerShell: List Empty Files
- PowerShell: Count Number of Files
- PowerShell: List Files by Name Pattern
- PowerShell: Filter File Name by Regular Expression
- PowerShell: List File by Size
- PowerShell: Sort Files by File Size 🚀
- PowerShell: List Files by Date Time
- PowerShell: Search Text in Files (grep)