Powershell: Path Tutorial

By Xah Lee. Date: . Last updated: .

Path parameter

Many commands has a -Path parameter.

# list file of a given path
dir c:/Users/xah/web/
# or
dir -Path c:/Users/xah/web/

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. For example, Resolve-Path "~/Saved Games" returns C:\Users\xah\Saved Games

Use -LiteralPath if you do not want expansion.

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.

PS C:\Users\xah> gl

Path
----
C:\Users\xah

Relative Path, Full Path

The -Path parameter accepts both full path and relative path. For example, the following are the same:

dir c:/Users/xah/Documents
cd c:/Users/xah/
dir Documents

Path expansion

Path allows Wildcards or other shortcut such as ~/ to mean home dir.

[see PowerShell: String Wildcards]

To see what a given path expand to, use Resolve-Path.

Resolve-Path ~/

# Path
# ----
# C:\Users\xah
Resolve-Path ~/Documents/*

# Path
# ----
# C:\Users\xah\Documents\Custom Office Templates
# C:\Users\xah\Documents\New folder (2)
# C:\Users\xah\Documents\PowerShell
# C:\Users\xah\Documents\Wolfram Desktop
# C:\Users\xah\Documents\wolframlang
# C:\Users\xah\Documents\wolframFunctions.txt

LiteralPath

The -LiteralPath parameter lets you input a path without expansion.

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

Check folder/file existence

Use test-path to check if a file/folder exist. It returns True or False.

# check if file/folder exist
test-path c:/Users/xah/web
# check if exist and is folder
test-path c:/Users/xah/web -PathType container
# check if exist and is file
test-path c:/Users/xah/web -PathType leaf

get dir path, file name, file extension

Split-Path -Parent path
return the fullpath of parent dir
Split-Path -Extension path
return the file extension
Split-Path -Leaf path
return just the file name
Split-Path -LeafBase path
return the file name sans extension
$x = c:/Users/xah/web/xahlee_info/powershell/powershell_path.html

Split-Path -Parent $x
# c:\Users\xah\web\xahlee_info\powershell

Split-Path -extension $x
# .html

Split-Path -leaf $x
# powershell_path.html

Split-Path -leafbase $x
# powershell_path


Split-Path -Qualifier $x
# c:

Split-Path -NoQualifier $x
# /Users/xah/web/xahlee_info/powershell/powershell_path.html

Join Path

This is useful in scripting. It frees you from worrying if the dir path ends in a slash.

join-path "a" "b"
# a\b

join-path "a/" "b"
# a\b

join-path "a" "/b"
# a\b

# duplicate separater
join-path "a/" "/b"
# a\b

# repeated separater are not removed
join-path "a/" "//b"
# a\\b

# drive path
join-path "c:" "b"
# c:\b

join-path "c:/" "/b"
# c:\b

save and restore current dir

These are useful in interactive use as well as in script.

Push-Location
Save current dir to a stack. (alias pushd)
Pop-Location
Restore (pop) dir from stack. (alias popd)

PowerShell

How-to

Advanced

Script Examples