Working with Files and Folders
The Log File That Taught Me Everything
Get-ChildItem -Path "\\server\logs" -Filter "*.log" -Recurse |
Select-String -Pattern "Exception|Error" -Context 2, 5 |
Export-Csv errors.csvUnderstanding Paths in PowerShell
Current Location
# Get current directory
Get-Location
pwd # Alias
# Change directory
Set-Location -Path "C:\Users"
cd "C:\Users" # Alias
# Go to parent directory
Set-Location ..
cd ..
# Go to home directory
Set-Location ~
cd ~
# Go back to previous location
Set-Location -
cd -
# Push and pop locations (directory stack)
Push-Location "C:\Windows" # Save current, go to new
Pop-Location # Return to savedPath Formats
Working with Paths
Navigating and Exploring
Listing Files and Folders
Advanced Filtering
Creating and Deleting
Creating Items
Deleting Items
Copying and Moving
Copying Files and Folders
Moving Files and Folders
Renaming Items
Reading Files
Reading Entire File
Reading Specific Encoding
Searching File Content
Writing Files
Writing Content
Writing Formatted Data
Working with CSV Files
Importing CSV
Exporting CSV
CSV Real-World Example
Working with JSON Files
Importing JSON
Exporting JSON
Working with XML Files
Importing XML
Creating XML
Real-World File Operations
Log Analysis Script
Backup Script
File Organization
Common Pitfalls
1. Forgetting to Use -Force for Nested Paths
2. Not Using -Raw When Needed
3. Forgetting -NoTypeInformation in CSV Export
4. Not Handling Errors in File Operations
Key Takeaways
What You've Learned
Next Steps
Last updated