The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Show the current locationGet-LocationView examples
List directory contentsGet-ChildItem -LiteralPath . -ForceView examples
Change directorySet-Location -LiteralPath 'C:\Projects'View examples
Move to the parentSet-Location ..View examples
Resolve an existing pathResolve-Path -LiteralPath '.\config.json'View examples
Create a directoryNew-Item -ItemType Directory -Path '.\reports'View examples
Create a text fileNew-Item -ItemType File -Path '.\notes.txt' -Value ` 'First line'View examples
Check whether a path existsTest-Path -LiteralPath '.\notes.txt' -PathType LeafView examples
Inspect one itemGet-Item -LiteralPath '.\notes.txt'View examples
Read a text fileGet-Content -LiteralPath '.\notes.txt'View examples
Read the last linesGet-Content -LiteralPath '.\app.log' -Tail 20View examples
Follow a growing logGet-Content -LiteralPath '.\app.log' -Tail 20 -WaitView examples
Copy a fileCopy-Item -LiteralPath '.\config.json' -Destination ` '.\config.backup.json'View examples
Copy a directory treeCopy-Item -LiteralPath '.\reports' -Destination ` '.\reports-backup' -RecurseView examples
Move or rename an itemMove-Item -LiteralPath '.\draft.txt' -Destination ` '.\final.txt'View examples
Find files recursivelyGet-ChildItem -Path . -Filter '*.json' -File -RecurseView examples
Filter files by sizeGet-ChildItem -Path . -File -Recurse | Where-Object Length -GT 10MBView examples
Search inside filesSelect-String -Path '.\*.log' -Pattern 'error' ` -CaseSensitive:$falseView examples
Preview file removalRemove-Item -LiteralPath '.\obsolete.txt' -WhatIfView examples
Preview recursive removalRemove-Item -LiteralPath '.\build-output' -Recurse ` -WhatIfView 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

01

Navigate with location and path objects

PowerShell maintains a current location for its active provider. Get-Location shows it, Set-Location changes it, and Resolve-Path verifies and normalizes an existing path. LiteralPath treats brackets and wildcard characters as ordinary filename characters.

Inspect and enter a project directory
Get-Location
Get-ChildItem -LiteralPath . -Force
Set-Location -LiteralPath 'C:\Projects'
Get-Location
Set-Location ..
Resolve an existing configuration path
$configPath = Resolve-Path -LiteralPath '.\config.json'
$configPath.Path

Note: Resolve-Path reports an error when the path does not exist. Use Test-Path first when absence is an expected condition.

Back to quick reference ↑
02

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.

Create a reports directory and notes file
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
Output
True
True

Note: Out-Null suppresses the created objects so this example prints only the two existence checks.

Back to quick reference ↑
03

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.

Inspect and read a text file
$file = Get-Item -LiteralPath '.\notes.txt'
$file.Name
$file.Length
Get-Content -LiteralPath $file.FullName
Read and follow an application log
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.

Back to quick reference ↑
04

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.

Back up and rename a configuration
Copy-Item -LiteralPath '.\config.json' -Destination '.\config.backup.json'
Move-Item -LiteralPath '.\config.json' -Destination '.\config.production.json'
Get-ChildItem -Path '.\config*.json'
Copy a directory tree
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.

Back to quick reference ↑
05

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.

Find large JSON files
Get-ChildItem -Path . -Filter '*.json' -File -Recurse |
  Where-Object Length -GT 10MB |
  Sort-Object Length -Descending |
  Select-Object FullName, Length
Search application logs for errors
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.

Back to quick reference ↑
06

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.

Preview and remove one file
$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.

Preview recursive directory removal
$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.

Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Microsoft LearnMicrosoft.PowerShell.Management Modulelearn.microsoft.com
  2. Microsoft LearnGet-ChildItemlearn.microsoft.com
  3. Microsoft LearnRemove-Itemlearn.microsoft.com

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback