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 PowerShell: Working with Directories

List Files

List file in current directory.

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

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

Show hidden files and system files

dir -Force

Recurse

# list file in current dir, and all subdirs
dir -recurse

Show file name only

# list file names
dir -recurse -name

Show file full path, don't truncate

PowerShell is very annoying. It truncate file path if your terminal is not wide enough.

# list file, show full path
# any of the following

dir -recurse | foreach {$_.fullname} | fl

dir -recurse | select -property fullname | fl

File Attributes (Modes) (Hidden, System, ReadOnly etc)

PowerShell: Get/Set File Attributes

List Files by File Name Pattern

by File Name Extension:

dir * -recurse -file -include *.jpg

Note: when you use -include, you must list by dir content, e.g. dir * or with -recurse

multiple extensions:

dir * -recurse -file -include *.jpg,*.jpeg

[see PowerShell: String Wildcards]

Emacs backup files (file name ending in ~):

dir * -recurse -file -include *~

Exclude by file name extension:

dir * -recurse -file -exclude *.jpg,*.jpeg,*.png,*.gif

Search file name by regex:

# list jpg or png files by regex
dir * -recurse -file | where { $_.name -match "\.jpg|\.png"}

[see PowerShell: String Operators]

List Empty Files

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

List Only Files, No Directory

dir -file -recurse

List File by Date Range

# list file with last write time greater than 24 hours ago
dir * -file  | Where {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
# list file within a date range

$datemin = [datetime]::ParseExact("2021-12-11 01:00:00", "yyyy-MM-dd hh:mm:ss", [Cultureinfo]::InvariantCulture)
$datemax = [datetime]::ParseExact("2021-12-13 00:00:00", "yyyy-MM-dd hh:mm:ss", [Cultureinfo]::InvariantCulture)

dir * -file -include "*.png"  | Where { $_.LastWriteTime -ge $datemin -and $_.LastWriteTime -le $datemax }

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

List File Creation Time

# list file with creation time
dir | sort CreationTime | Format-Table Name, CreationTime
# list files whose creation date is greater than x
dir | where { $_.CreationTime -gt [datetime]"2014/05/28" } | sort CreationTime | Format-Table Name, CreationTime

Delete File by Wildcard Pattern

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

PowerShell

Practical How-To

PowerShell in Depth

Programing PowerShell

Script Examples