Managing Files and Directories¶
PowerShell provides a rich set of cmdlets for working with the file system. These cmdlets let you create, read, modify, move, copy, and delete files and directories in a consistent, object‑oriented way. Because PowerShell treats the file system as a provider, you interact with it just like any other data source—using commands that return structured objects rather than plain text.
This section covers the essential operations every PowerShell user must know.
1. Navigating the File System¶
PowerShell uses familiar commands for navigation, similar to traditional shells.
1.1 Changing directories¶
Set-Location "C:\Projects"
# or
cd C:\Projects
1.2 Listing directory contents¶
Get-ChildItem
Examples:
Get-ChildItem C:\Logs
Get-ChildItem -Recurse
Get-ChildItem -Filter "*.txt"
Get-ChildItem returns objects, not text—each file has properties like Name, Length, LastWriteTime, etc.
2. Creating Files and Directories¶
2.1 Creating a directory¶
New-Item -ItemType Directory -Path "C:\Data\Reports"
2.2 Creating a file¶
New-Item -ItemType File -Path "C:\Data\notes.txt"
2.3 Creating a file with content¶
Set-Content -Path "C:\Data\hello.txt" -Value "Hello world"
3. Reading and Writing Files¶
3.1 Reading file content¶
Get-Content "C:\Data\hello.txt"
This returns each line as a string.
3.2 Writing content (overwrites file)¶
Set-Content -Path "C:\Data\log.txt" -Value "Starting process..."
3.3 Appending content¶
Add-Content -Path "C:\Data\log.txt" -Value "Process completed."
3.4 Reading entire file as a single string¶
Get-Content "C:\Data\config.json" -Raw
4. Copying, Moving, and Renaming¶
4.1 Copying files¶
Copy-Item "C:\Data\file.txt" -Destination "C:\Backup"
4.2 Copying directories recursively¶
Copy-Item "C:\Data" -Destination "D:\Archive" -Recurse
4.3 Moving files¶
Move-Item "C:\Data\old.txt" -Destination "C:\Data\Archive"
4.4 Renaming files¶
Rename-Item "C:\Data\report.txt" -NewName "report-old.txt"
5. Deleting Files and Directories¶
5.1 Deleting a file¶
Remove-Item "C:\Data\temp.txt"
5.2 Deleting a directory¶
Remove-Item "C:\Data\OldReports" -Recurse
5.3 Safe deletion with confirmation¶
Remove-Item "C:\Data\important.txt" -Confirm
6. Filtering and Searching¶
6.1 Filtering by name¶
Get-ChildItem -Filter "*.log"
6.2 Searching recursively¶
Get-ChildItem -Recurse -Filter "*.json"
6.3 Searching by content¶
Select-String -Path "C:\Logs\*.txt" -Pattern "ERROR"
This returns objects with:
- File name
- Line number
- Matching text
7. Working With File Objects¶
Because PowerShell returns objects, you can inspect properties directly.
$file = Get-ChildItem "C:\Data\report.txt"
$file.Name
$file.Length
$file.LastWriteTime
You can pipe file objects into other commands:
Get-ChildItem *.log | Sort-Object Length -Descending
8. Using the File System Provider¶
PowerShell treats the file system like a drive:
Get-PSDrive
You can create your own drives:
New-PSDrive -Name Logs -PSProvider FileSystem -Root "C:\Logs"
Then access it like:
cd Logs:
9. Practical Examples¶
9.1 Cleaning up old log files¶
Get-ChildItem "C:\Logs" -Filter "*.log" |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item
9.2 Backing up configuration files¶
Copy-Item "C:\App\Config\*.json" -Destination "D:\Backup" -Recurse
9.3 Creating a directory structure¶
"Input","Output","Archive" | ForEach-Object {
New-Item -ItemType Directory -Path "C:\Data\$_"
}
10. Summary¶
| Task | Cmdlet |
|---|---|
| List files | Get-ChildItem |
| Create file/directory | New-Item |
| Read file | Get-Content |
| Write file | Set-Content |
| Append file | Add-Content |
| Copy | Copy-Item |
| Move | Move-Item |
| Delete | Remove-Item |
| Search content | Select-String |
PowerShell’s file‑system cmdlets are powerful, consistent, and object‑oriented, making them ideal for automation and scripting.