PowerShell: List Files

By Xah Lee. Date: . Last updated: .

Command names and parameter names are case-insensitive.

This page show commands that list files. To list dir, see Working with Directories

List Files

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

# if no -path given, default to current dir
dir

list file of a given path:

dir c:/Users/xah/web/

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

Wildcard in Path and Literal Path

Path can contain Wildcards * to stand for any char, zero or more times:

# list files whose name contain index
dir c:/Users/xah/web/*index*

# list files whose name ends in jpg
dir c:/Users/xah/web/*jpg

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

# list files in a dir, dir name literally is asterisk j p g
dir -LiteralPath c:/Users/xah/web/*jpg

[see Path Tutorial]

Show Subdirectories (Recurse and Depth)

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

-recurse has a alias of -s

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

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 * -recurse -file | where {$_.length -eq 0}

List Only Files, No Directory

dir -file -recurse

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

Practical How-To

list files

list dirs

on dir

on file

Windows config

misc


PowerShell in Depth

Programing PowerShell

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples