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
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.logTrue
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:
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:
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, Lengthcss
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
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:
$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
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
>andOut-File, so pass-Encoding utf8when 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-Expressiontrap.
Test yourself
What is the difference between Set-Content and Add-Content?
- None
- Set-Content replaces the file's content, Add-Content appends to it
- Add-Content creates folders
- Set-Content only works on new files
What does Get-Content file.txt return for a 3-line file?
- One string
- An array of 3 strings
- A FileInfo object
- The number 3
Which is the portable way to build the path logs/today.log?
- 'logs' + '\' + 'today.log'
- Join-Path 'logs' 'today.log'
- 'logs/today.log'.Combine()
- Split-Path logs today.log