TExcelTAD
Home › PowerShell › PowerShell Files & Folders

PowerShell Files & Folders

Creating, reading, copying and cleaning up files is one of the most common jobs for PowerShell. The cmdlets use the generic noun Item for files and folders, plus Content for what's inside a file. All the examples here run in /tmp; the same commands work on Windows with paths like C:\Temp.

Task Cmdlet Common alias
list folder contents Get-ChildItem gci, dir
create file or folder New-Item ni
write / append text Set-Content / Add-Content sc / ac
read text Get-Content gc, type
check existence Test-Path
copy, move, rename Copy-Item, Move-Item, Rename-Item copy, move, ren
delete Remove-Item del
build paths Join-Path, Split-Path, Resolve-Path

Creating files and folders

powershell · try it yourself
New-Item -ItemType Directory -Path ./project/logs -Force | Out-Null
New-Item -ItemType File -Path ./project/readme.md -Value "# Project`n" | Out-Null
Set-Content -Path ./project/logs/app.log -Value 'started', 'running'
Add-Content -Path ./project/logs/app.log -Value 'stopped'

Test-Path ./project/readme.md
Test-Path ./project/logs -PathType Container
Get-Content ./project/logs/app.log
True
True
started
running
stopped

Set-Content replaces the whole file; Add-Content appends. Passing an array writes one line per element. Test-Path -PathType Leaf checks for a file, Container for a folder.

Reading files

Get-Content returns an array of lines by default. -Raw returns the whole file as a single string:

powershell · try it yourself
Set-Content ./servers.txt 'web01', 'web02', 'db01', 'cache01', 'backup01'

$lines = Get-Content ./servers.txt
$lines.Count
$lines[2]
Get-Content ./servers.txt -TotalCount 2           # first 2 lines (like head)
Get-Content ./servers.txt -Tail 1                 # last line (like tail)
(Get-Content ./servers.txt -Raw).Length
Get-Content ./servers.txt | Where-Object { $_ -like 'web*' }
5
db01
web01
web02
backup01
34
web01
web02

-Tail with -Wait follows a growing log file live, like tail -f.

Listing folders

Get-ChildItem returns FileInfo and DirectoryInfo objects. Filter with -Filter, -Include, -File, -Directory and -Recurse:

powershell · try it yourself
New-Item -ItemType Directory ./site/css, ./site/js -Force | Out-Null
'index.html', 'about.html', 'css/main.css', 'js/app.js', 'js/util.js' |
    ForEach-Object { Set-Content "./site/$_" "content of $_" }

Get-ChildItem ./site -Name
'---'
Get-ChildItem ./site -Recurse -Filter *.js | ForEach-Object Name
'---'
Get-ChildItem ./site -Directory | ForEach-Object Name
'---'
Get-ChildItem ./site -Recurse -File |
    Sort-Object Length -Descending |
    Select-Object -First 2 Name, Length
css
js
about.html
index.html
---
app.js
util.js
---
css
js
---

Name       Length
----       ------
main.css       24
about.html     22

-Name returns just the names as strings. -Filter is applied by the file system and is the fastest way to filter by wildcard.

Copy, move, rename, delete

powershell · try it yourself
New-Item -ItemType Directory ./work, ./archive -Force | Out-Null
Set-Content ./work/report-jan.txt 'January'
Set-Content ./work/report-feb.txt 'February'
Set-Content ./work/temp.tmp 'scratch'

Copy-Item ./work/report-jan.txt ./archive/
Move-Item ./work/report-feb.txt ./archive/
Rename-Item ./archive/report-jan.txt -NewName 2026-01.txt
Remove-Item ./work/*.tmp

"work:    $((Get-ChildItem ./work -Name) -join ', ')"
"archive: $((Get-ChildItem ./archive -Name) -join ', ')"
work:    report-jan.txt
archive: 2026-01.txt, report-feb.txt

To copy or delete a folder with everything inside, add -Recurse. And remember -WhatIf before running a destructive command with wildcards.

Working with paths

Never glue paths together with string concatenation; use the path cmdlets, which pick the right separator on every OS:

powershell · try it yourself
$base = '/var/log/myapp'
Join-Path $base 'errors' 'today.log'
Split-Path '/var/log/myapp/errors.log' -Leaf
Split-Path '/var/log/myapp/errors.log' -Parent
[System.IO.Path]::GetFileNameWithoutExtension('backup.tar.gz')
[System.IO.Path]::GetExtension('backup.tar.gz')
[System.IO.Path]::ChangeExtension('notes.txt', '.md')
/var/log/myapp/errors/today.log
errors.log
/var/log/myapp
backup.tar
.gz
notes.md

File properties and hashes

powershell · try it yourself
Set-Content ./data.csv "id,name`n1,Ada`n2,Linus" -NoNewline
$file = Get-Item ./data.csv
"$($file.Name): $($file.Length) bytes, extension $($file.Extension)"
$file.BaseName
(Get-FileHash ./data.csv -Algorithm SHA256).Hash.Substring(0, 16)
data.csv: 21 bytes, extension .csv
data
C946A6CF06830095

Get-FileHash is handy for checking that a download or a copy is identical to the original.

Note: Text encoding: PowerShell 7 writes UTF-8 without BOM by default. Windows PowerShell 5.1 used UTF-16 for > and Out-File, so pass -Encoding utf8 when a script must produce the same files on both.

Go further: PowerShell Objects and Get-FileHash builds a hashed file inventory and explains the Invoke-Expression trap.

Test yourself

What is the difference between Set-Content and Add-Content?

  1. None
  2. Set-Content replaces the file's content, Add-Content appends to it
  3. Add-Content creates folders
  4. Set-Content only works on new files

What does Get-Content file.txt return for a 3-line file?

  1. One string
  2. An array of 3 strings
  3. A FileInfo object
  4. The number 3

Which is the portable way to build the path logs/today.log?

  1. 'logs' + '\' + 'today.log'
  2. Join-Path 'logs' 'today.log'
  3. 'logs/today.log'.Combine()
  4. Split-Path logs today.log