The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Show the current location | Get-Location | View examples |
| List directory contents | Get-ChildItem -LiteralPath . -Force | View examples |
| Change directory | Set-Location -LiteralPath 'C:\Projects' | View examples |
| Move to the parent | Set-Location .. | View examples |
| Resolve an existing path | Resolve-Path -LiteralPath '.\config.json' | View examples |
| Create a directory | New-Item -ItemType Directory -Path '.\reports' | View examples |
| Create a text file | New-Item -ItemType File -Path '.\notes.txt' -Value `
'First line' | View examples |
| Check whether a path exists | Test-Path -LiteralPath '.\notes.txt' -PathType Leaf | View examples |
| Inspect one item | Get-Item -LiteralPath '.\notes.txt' | View examples |
| Read a text file | Get-Content -LiteralPath '.\notes.txt' | View examples |
| Read the last lines | Get-Content -LiteralPath '.\app.log' -Tail 20 | View examples |
| Follow a growing log | Get-Content -LiteralPath '.\app.log' -Tail 20 -Wait | View examples |
| Copy a file | Copy-Item -LiteralPath '.\config.json' -Destination `
'.\config.backup.json' | View examples |
| Copy a directory tree | Copy-Item -LiteralPath '.\reports' -Destination `
'.\reports-backup' -Recurse | View examples |
| Move or rename an item | Move-Item -LiteralPath '.\draft.txt' -Destination `
'.\final.txt' | View examples |
| Find files recursively | Get-ChildItem -Path . -Filter '*.json' -File -Recurse | View examples |
| Filter files by size | Get-ChildItem -Path . -File -Recurse |
Where-Object Length -GT 10MB | View examples |
| Search inside files | Select-String -Path '.\*.log' -Pattern 'error' `
-CaseSensitive:$false | View examples |
| Preview file removal | Remove-Item -LiteralPath '.\obsolete.txt' -WhatIf | View examples |
| Preview recursive removal | Remove-Item -LiteralPath '.\build-output' -Recurse `
-WhatIf | View examples |
PowerShell file commands return objects rather than preformatted text. Prefer full cmdlet names in scripts, use LiteralPath for paths that may contain wildcard characters, and inspect resolved targets before any recursive removal.
Step by step
Detailed examples
Create files and directories predictably
New-Item creates filesystem objects and emits the created item. Test-Path answers whether an item exists, while PathType can distinguish a file from a container before later commands act on it.
New-Item -ItemType Directory -Path '.\reports' | Out-Null
New-Item -ItemType File -Path '.\notes.txt' -Value 'First line' | Out-Null
Test-Path -LiteralPath '.\reports' -PathType Container
Test-Path -LiteralPath '.\notes.txt' -PathType Leaf True
TrueNote: Out-Null suppresses the created objects so this example prints only the two existence checks.
Inspect metadata and text content
Get-Item returns metadata for one filesystem item. Get-Content emits text line by line by default, supports focused tail reads, and can wait for appended log content in a long-running session.
$file = Get-Item -LiteralPath '.\notes.txt'
$file.Name
$file.Length
Get-Content -LiteralPath $file.FullName Get-Content -LiteralPath '.\app.log' -Tail 20
Get-Content -LiteralPath '.\app.log' -Tail 20 -Wait Note: The command using -Wait continues until interrupted with Ctrl+C. It is intended for text logs, not arbitrary binary files.
Copy and move exact targets
Copy-Item leaves the source in place, while Move-Item relocates it and can rename it. Use Recurse for directory contents and decide explicitly whether an existing destination may be replaced before adding Force.
Copy-Item -LiteralPath '.\config.json' -Destination '.\config.backup.json'
Move-Item -LiteralPath '.\config.json' -Destination '.\config.production.json'
Get-ChildItem -Path '.\config*.json' Copy-Item -LiteralPath '.\reports' -Destination '.\reports-backup' -Recurse
Get-ChildItem -LiteralPath '.\reports-backup' -Recurse Note: Destination behavior changes when a target directory already exists. Inspect and test with temporary data before using Force in automation.
Search file objects and their content
Get-ChildItem emits FileInfo and DirectoryInfo objects, so properties such as Length can be filtered without parsing display text. Select-String searches textual content and returns MatchInfo objects containing paths, line numbers, and matching lines.
Get-ChildItem -Path . -Filter '*.json' -File -Recurse |
Where-Object Length -GT 10MB |
Sort-Object Length -Descending |
Select-Object FullName, Length Select-String -Path '.\*.log' -Pattern 'error' -CaseSensitive:$false |
Select-Object Path, LineNumber, Line Note: Select-String is case-insensitive by default. The explicit CaseSensitive switch value makes the intended behavior visible.
Preview removal before changing the filesystem
Remove-Item deletes files and directories rather than moving them to the Recycle Bin. WhatIf displays the planned action without performing it. Re-run without WhatIf only after checking that the resolved absolute targets stay within the intended directory.
$target = Resolve-Path -LiteralPath '.\obsolete.txt'
$target.Path
Remove-Item -LiteralPath $target.Path -WhatIf
# After verifying the preview and resolved path:
Remove-Item -LiteralPath $target.Path Note: Removed files may not be recoverable. Keep the preview and the real removal as separate reviewed steps in important scripts.
$target = Resolve-Path -LiteralPath '.\build-output'
Get-ChildItem -LiteralPath $target.Path -Force
Remove-Item -LiteralPath $target.Path -Recurse -WhatIf Note: Do not remove -WhatIf until the resolved path and listed contents have been verified.
Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



