PowerShell: Path Tutorial

By Xah Lee. Date: . Last updated: .

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:/Program Files/"

# or
dir -Path "c:/Program Files/"

Path Value is String Type

Path value is string type. It can be single quoted or double quoted.

[see PowerShell: String]

Normally, quote can be omitted, but if path contains space, it must be quoted.

dir "c:/Program Files/"
dir 'c:/Program Files/'

Double Quoted String Goes Thru String Expansion

$xx = "Files"
dir "c:/Program $xx/"

# same as
dir "c:/Program Files/"

Path Value Are Expanded by Path Expansion

Path value can contain abbrev, such as beginning ~ to mean $home, and String Wildcards such as * for any characters.

dir '~/Documents/'

# same as
dir "$home/Documents/"

Note: $home is one of the pre-defined variable. [see PowerShell: Automatic Variables]

Use parameter -LiteralPath if you do not want Path Expansion.

[see PowerShell: Path Expansion (get fullpath)]

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:

single dot, double dots

For example:

current dir

Get-Location (alias gl, pwd) return the current dir.

Path expansion

LiteralPath

The -LiteralPath parameter lets you input a path without Path Expansion . (double quoted string still go tru string expansion. [see PowerShell: String])

# list children of a dir named math* as is
dir -LiteralPath "math*"

PowerShell Path


PowerShell in Depth

Path

Pipe

Comment

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples

Misc