PowerShell: List Files

By Xah Lee. Date: . Last updated: .

💡 TIP: Command names and parameter names are case-insensitive.

List Files

List file in current directory. (dir is alias of Get-ChildItem)

# list files in current dir
dir

# full path
dir "c:/Program Files/Windows Defender"

# relative path
dir Documents

# using home dir abbrev
dir ~/Documents/

If path contain space, need quote. [see Path Tutorial]

Wildcard in Path

Path can contain Wildcards such as *.

# list files whose name ends in jpg in current dir
dir *jpg

[see Path Tutorial]

LiteralPath

if you do not want special interpretation of path, use -LiteralPath.

# list files in a dir whose name is just a asterisk
dir -LiteralPath '*'

Show Subdirectories (Recurse and Depth)

list files in current dir, and all subdirectories, any depth:

-recurse

dir -recurse

-depth 1 means include children of first-level subdirectories. -depth 2 means up to second-level subdirectories, etc.

dir -depth 1

-depth 0 is same as no recurse.

# list just current dir children
dir -depth 0

Show file name only

# list file names
dir -recurse -name
# result paths are relative to current dir

Show file full path, don't truncate

List file by file name pattern

Show Hidden Files and System Files

dir -Force

[see Get/Set File Attributes]

List Empty Files

dir -file | where {$_.length -eq 0}

List Only Files, No Directory

dir -file

List File by Date Range, Creation Time

Count Number of Files

# count number of files in current dir and subdirs
dir -file -recurse | measure
# count number of html files in current dir and subdirs
dir -recurse -include *.html  | measure

Delete File by Wildcard Pattern

# delete macOS folder preference file
dir -recurse -Force -filter .DS_Store | rm -Force
# delete emacs backup. file name ending in ~
dir -recurse -file -filter *~ | rm

[see List File by File Name Pattern]

PowerShell, List Files