Filesystem
By the end of this lesson you'll be able to list, create, copy, move, rename, read, write, and safely delete files and folders from PowerShell — the everyday skills that make the shell faster than clicking around in File Explorer.
Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn
💡 Real-World Analogy
Think of PowerShell as a universal remote control for storage. A normal remote only works with one TV; PowerShell's remote works with any device that speaks the same protocol. That protocol is the provider model: the file system, the Windows Registry, environment variables and certificates are all exposed as lettered "drives" ( C: , HKCU: , Env: , Cert: ). Because they all share one interface, the same verbs — Get-ChildItem , Set-Location , New-Item , Remove-Item — drive every one of them. Learn the buttons once and they work everywhere.
1. The Provider Model
A provider is an adapter that makes some data store look like a disk drive, so you can browse it with file-style commands. Get-PSProvider lists the adapters; Get-PSDrive lists the drives they expose. The big payoff: once you learn the file-system cmdlets below, the exact same commands also work on the registry and environment variables — you just point them at a different drive.
2. Listing & Navigating
Get-ChildItem shows what's inside a folder — its aliases gci , ls and dir all run the same cmdlet, so use whichever feels natural. Set-Location (alias cd ) moves you into a folder, and Get-Location (alias pwd ) tells you where you are. Add -Recurse to dig into every sub-folder and -Filter to match a name pattern.
3. Create, Copy, Move & Check
New-Item makes both files and folders — the -ItemType switch decides which. Copy-Item duplicates a file; Move-Item relocates it, and when the destination is just a new name in the same folder it acts as a rename (so you rarely need Rename-Item ). Test-Path returns a plain True or False , which makes it perfect for an if check before you create or delete anything.
Now you try. The script below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it and check your output against the expected lines.
4. Reading & Writing File Content
Three cmdlets cover almost everything: Set-Content writes the file (replacing whatever was there), Add-Content appends a new line to the end, and Get-Content reads it back. One thing surprises everyone: Get-Content returns an array of lines , not a single block of text — so (Get-Content file).Count is the line count and [0] is the first line.
5. Deleting — Safely
Remove-Item deletes files and folders. To remove a folder and its contents you must add -Recurse — and that is exactly where accidents happen, because there is no recycle bin to undo it. The safety habit: run the command first with -WhatIf , which prints every action it would take without changing a single byte on disk. When the preview looks right, run it again without -WhatIf .
Your turn again. Guard the delete with Test-Path so it only runs when the folder exists, and preview it safely. Fill in the two blanks:
Common Errors (and the fix)
- Deleting a whole tree by accident: Remove-Item .\\folder -Recurse wipes everything inside with no undo. Fix: run it once with -WhatIf first to preview, then remove the switch.
- "A positional parameter cannot be found": usually a path with spaces that isn't quoted, so PowerShell splits it. Fix: quote the path — Get-Content "C:\\My Reports\\june.txt" .
- Treating Get-Content as one string: it returns an array of lines , so string operations behave oddly. Fix: use [0] for the first line, .Count for the line count, or Get-Content -Raw for the whole file as one string.
- "Cannot remove item ... it is being used": the file is open in another program. Fix: close the program (or stop the process) holding the file, then retry.
- New-Item creates the wrong thing: leaving off -ItemType or mistyping it. Fix: be explicit — -ItemType File or -ItemType Directory .
Pro Tips
- 💡 Preview destructive commands with -WhatIf ; PowerShell supports it on Remove-Item , Move-Item , Copy-Item and more.
- 💡 Quote every path. It costs nothing and saves you the day a folder has a space in its name.
- 💡 Live-tail a log with Get-Content app.log -Wait — it's PowerShell's tail -f .
- 💡 Pipe into Select-String to grep through files: Get-ChildItem -Recurse -Filter *.ps1 | Select-String "TODO" .
📋 Quick Reference
Task
Cmdlet (alias)
Example
List a folder
Get-ChildItem (ls)
Get-ChildItem C:\\Projects
Change folder
Set-Location (cd)
Set-Location .\\reports
New folder
New-Item -ItemType Directory
New-Item -ItemType Directory -Path .\\logs
New file
New-Item -ItemType File
New-Item -ItemType File -Path a.txt
Copy / Move / Rename
Copy-Item / Move-Item
Move-Item a.txt b.txt
Write / Append / Read
Set- / Add- / Get-Content
Add-Content log.txt "line"
Path exists?
Test-Path
Test-Path .\\logs
Delete (preview first!)
Remove-Item -Recurse -WhatIf
Remove-Item .\\logs -Recurse -WhatIf
Frequently Asked Questions
Mini-Challenge: a Daily Log Tool
No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check the result against the comment. This is exactly the kind of small script that automates a real chore.
🎉 Lesson Complete!
- ✅ One set of cmdlets works across every provider drive ( C: , HKCU: , Env: )
- ✅ Get-ChildItem lists, Set-Location navigates, Get-Location tells you where you are
- ✅ New-Item -ItemType creates files or folders; Copy-Item / Move-Item duplicate, move and rename
- ✅ Set-Content writes, Add-Content appends, Get-Content reads (as an array of lines)
- ✅ Test-Path guards your actions; Remove-Item -Recurse deletes — always preview with -WhatIf first
- ✅ Next lesson: Automation and Task Scheduling — run your scripts on a schedule
Practice quiz
What is a PowerShell provider?
- An adapter that exposes a data store as a drive
- A type of variable
- A remote server
- A kind of loop
Answer: An adapter that exposes a data store as a drive. Providers expose stores like FileSystem and Registry as lettered drives.
Which cmdlet lists the contents of a folder?
- Show-Folder
- Get-ChildItem
- List-Items
- Read-Directory
Answer: Get-ChildItem. Get-ChildItem (aliases gci, ls, dir) lists what is inside a folder.
Which cmdlet changes your current location, like cd?
- Move-Path
- Go-Folder
- Set-Location
- Change-Dir
Answer: Set-Location. Set-Location (alias cd) changes the current working folder.
How do you create a new folder with New-Item?
- New-Item -Folder
- New-Item -Type Dir
- New-Item -Make
- New-Item -ItemType Directory
Answer: New-Item -ItemType Directory. New-Item -ItemType Directory creates a folder; -ItemType File creates a file.
Which cmdlet appends a line to the end of a file?
- Add-Content
- Set-Content
- Get-Content
- Push-Content
Answer: Add-Content. Add-Content appends; Set-Content overwrites the whole file.
What does Get-Content return for a multi-line text file?
- A single string
- An array of lines (one per line)
- A hashtable
- Nothing
Answer: An array of lines (one per line). Get-Content returns an array with one element per line.
Which cmdlet returns True or False for whether a path exists?
- Check-Path
- Exists-Item
- Test-Path
- Find-Path
Answer: Test-Path. Test-Path returns a Boolean, ideal for an if guard before acting.
What does the -WhatIf switch do on Remove-Item?
- Deletes faster
- Skips confirmation
- Recurses automatically
- Previews the action without changing anything
Answer: Previews the action without changing anything. -WhatIf shows what would happen but makes no changes on disk.
Which switch is required to delete a folder and its contents?
- -Recurse
- -Force
- -All
- -Deep
Answer: -Recurse. Remove-Item needs -Recurse to delete a folder along with everything inside.
Why should you quote file paths that contain spaces?
- It runs faster
- PowerShell would otherwise split the path at the space
- Quotes hide the path
- It is never necessary
Answer: PowerShell would otherwise split the path at the space. Without quotes PowerShell treats the space as a separator and misreads the path.
Continue this course
- Previous: Functions and Scripts
- Next: Automation and Task Scheduling