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.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
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.
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:
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.
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.