Files, folders and providers
Navigate and manage files with the *-Item and *-Content cmdlets, and explore other drives like Env:.
- Move around with Get-Location and Set-Location, and list with Get-ChildItem
- Create, copy, move, rename and remove items - safely, with -WhatIf
- Read and write files with Get-Content, Set-Content and Add-Content, and use providers like Env:
PowerShell manages files with a small family of cmdlets that share nouns:
| Cmdlet | Does | Familiar alias |
|---|---|---|
Get-Location | where am I? | pwd |
Set-Location | go somewhere | cd |
Get-ChildItem | list (-Recurse, -Filter *.log, -File, -Directory, -Force for hidden) | ls, dir, gci |
New-Item | create a file or folder (-ItemType Directory) | ni, mkdir |
Copy-Item / Move-Item / Rename-Item | copy, move, rename | cp, mv, ren |
Remove-Item | delete (-Recurse for folders) | rm, del |
Test-Path | does it exist? ($true/$false) | |
Join-Path | build a path with the right separator |
Paths use \ on Windows and / elsewhere - and PowerShell accepts either on Windows. Join-Path picks the right one, which keeps scripts portable.
1$root = Join-Path ([IO.Path]::GetTempPath()) "emberfall-$(Get-Random)"
2New-Item -ItemType Directory -Path $root | Out-Null
3Set-Location $root
4New-Item -ItemType Directory -Path 'north-tower', 'cave' | Out-Null
5New-Item -ItemType File -Path 'cave/mossback.txt', 'cave/pebble.txt' | Out-Null
6Copy-Item 'cave/pebble.txt' 'north-tower/pebble-backup.txt'
7Rename-Item 'cave/mossback.txt' 'old-mossback.txt'
8Get-ChildItem -Recurse -File | Sort-Object Name | ForEach-Object Name
9Test-Path 'cave/mossback.txt'
10(Get-ChildItem -Directory).Countold-mossback.txt pebble-backup.txt pebble.txt False 2
Two things to notice:
| Out-Nullthrows away output you don’t want -New-Itemwould otherwise print a listing of everything it created.- The script never prints a full path, because temporary folders have random names. It prints
Names instead.
Reading and writing files
Set-Content file.txt 'text'writes (replacing what was there);Add-Contentappends.Get-Content file.txtreturns the file as an array of lines - so(Get-Content file.txt).Countcounts lines and[0]is the first one.-Rawgives one big string instead;-TotalCount 5reads just the first 5 lines and-Tail 5the last 5.- Careful: a file with exactly one line comes back as a plain string, not an array, so
[0]would give its first character. Wrap the call in the array operator,@(Get-Content file.txt), whenever you index into the lines. Out-Fileand the redirection operators>and>>save formatted output, like the console shows it. For data, preferSet-Content, orExport-CsvandConvertTo-Json(coming up).
1$log = Join-Path ([IO.Path]::GetTempPath()) "feeding-$(Get-Random).txt"
2Set-Content -Path $log -Value '07:00 Ember: 4 goats'
3Add-Content -Path $log -Value '07:30 Glim: 1 lantern moth', '08:00 Skyla: 6 fish'
4$lines = Get-Content -Path $log
5"$($lines.Count) feedings, first: $($lines[0])"
6Get-Content -Path $log -Tail 1
7Remove-Item $log
8Test-Path $log3 feedings, first: 07:00 Ember: 4 goats 08:00 Skyla: 6 fish False
Providers: drives that aren’t disks
The same *-Item cmdlets work on more than files. Providers present other data stores as drives:
| Drive | Contains | Try |
|---|---|---|
C:, / | the filesystem | Get-ChildItem C:\ |
Env: | environment variables | Get-ChildItem Env:, $env:PATH |
Variable: | your PowerShell variables | Get-ChildItem Variable: |
Function: | loaded functions | Get-ChildItem Function: |
HKLM:, HKCU: | the Windows registry | Get-ChildItem HKCU:\Software |
Cert: | Windows certificate stores | Get-ChildItem Cert:\CurrentUser\My |
Get-PSDrive lists them all. Environment variables are handy enough to have their own syntax: $env:NAME reads one and $env:NAME = 'value' sets it for this session.
1$env:SANCTUARY = 'Emberfall'
2"Hello from $env:SANCTUARY"
3Test-Path Env:SANCTUARY
4(Get-Item Env:SANCTUARY).Value.Length
5Remove-Item Env:SANCTUARY
6Test-Path Env:SANCTUARYHello from Emberfall True 9 False
Key takeaways
Get/Set-Location, Get-ChildItem and the *-Item cmdlets navigate and manage files.
Use
-WhatIfto rehearse changes andJoin-Pathto build portable paths.Get-Content returns lines; Set-Content replaces, Add-Content appends.
Providers expose Env:, Variable:, the registry and more as drives with the same cmdlets.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write PowerShell scripts
Write a script in the editor and run it for real against sample input, which is piped into your script as $input. Scripts run on PowerShell 6.2 through Try It Online (tio.run), a free public service, so these exercises avoid PowerShell 7-only syntax; your script and test input are sent there.
Build the enclosures
The starter creates an empty sanctuary folder and moves into it. Create:
- folders
volcanoandlagoon - empty files
volcano/ember.txt,volcano/cinder.txtandlagoon/skyla.txt
Then the provided lines print every file name (sorted) and the number of folders. Hide New-Item’s own output with | Out-Null.
- Builds the enclosures
Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.
Feeding journal
Each input line is a feeding record. Write them all to a journal file in a temporary folder - the first with Set-Content, the rest with Add-Content, or all at once - then print entries: N, the first line and the last line of the file, read back with Get-Content.
- Morning feedings
- One feeding
Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…