Navigating and Managing Files
Prerequisites: The pipeline
PowerShell's file cmdlets work the same on every operating system, and
the familiar aliases (cd, ls, mkdir, cp, mv, rm) are available
too.
Creating files and folders
New-Item -ItemType Directory -Path 'reports', 'archive/2025' -Force | Out-Null
New-Item -ItemType File -Path 'notes.txt' | Out-Null
Set-Content -Path 'reports/jan.csv' -Value 'region,total'
Get-ChildItem -Name | Sort-Object
archive
notes.txt
reports
New-Item -ItemType Directorycreates folders;-Forcecreates missing parents and doesn't fail if they exist.Set-Contentwrites text to a file (the Files lesson in Level 2 covers reading and writing in depth).Get-ChildItem -Namelists just the names. (| Out-Nullhides the objectsNew-Itemwould otherwise print.)
Copying, moving, renaming, and deleting
Set-Content report.txt 'draft'
Copy-Item report.txt report-backup.txt
Rename-Item report.txt final-report.txt
New-Item -ItemType Directory docs | Out-Null
Move-Item final-report.txt docs/
Remove-Item report-backup.txt
Get-ChildItem -Recurse -Name | Sort-Object
docs
docs\final-report.txt
Remove-Item -Recurse deletes a folder and its contents. Deleted items
don't go to the Recycle Bin.
-WhatIf and -Confirm
Cmdlets that change things support -WhatIf — describe what would
happen, without doing it:
'a.log', 'b.log', 'keep.txt' | ForEach-Object { New-Item $_ | Out-Null }
Remove-Item *.log -WhatIf
Get-ChildItem -Name | Sort-Object
What if: Performing the operation "Remove File" on target "C:\demo\a.log".
What if: Performing the operation "Remove File" on target "C:\demo\b.log".
a.log
b.log
keep.txt
Nothing was deleted. Use -WhatIf before any bulk change, and
-Confirm to be asked about each item.
Finding files
New-Item -ItemType Directory -Path src/utils -Force | Out-Null
'src/app.ps1', 'src/utils/math.ps1', 'src/readme.md' | ForEach-Object { New-Item $_ | Out-Null }
Get-ChildItem -Path src -Recurse -Filter *.ps1 | Select-Object -ExpandProperty Name
(Get-ChildItem -Path src -Recurse -File).Count
Test-Path src/readme.md
Test-Path src/missing.md
app.ps1
math.ps1
3
True
False
Test-Path returns True or False — perfect in conditions.
Paths
Join-Path builds paths with the right separator for the operating
system, and Split-Path takes them apart:
Join-Path 'reports' '2025' 'april.csv'
Split-Path 'reports/2025/april.csv' -Leaf
[System.IO.Path]::GetExtension('april.csv')
reports\2025\april.csv
april.csv
.csv
(The output uses \ on Windows and / on Linux and macOS. PowerShell
accepts either when you type paths.)