Working with Files and Folders

The Log File That Taught Me Everything

Three years ago, I was troubleshooting a mysterious application crash that only happened in production. The only clue was buried in 50GB of log files spread across multiple servers, spanning 3 months. Manual searching was impossible.

I wrote my first PowerShell script to search those logs:

Get-ChildItem -Path "\\server\logs" -Filter "*.log" -Recurse |
    Select-String -Pattern "Exception|Error" -Context 2, 5 |
    Export-Csv errors.csv

In 15 minutes, I had every error from 3 months of logs with context lines. The pattern that emerged led me to the root cause within an hour. That script saved me days of manual log review and taught me the power of automated file operations.

Understanding Paths in PowerShell

PowerShell works with file system paths across Windows, macOS, and Linux.

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 saved

Path Formats

Working with Paths

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

CSV files are perfect for tabular data.

Importing CSV

Exporting CSV

CSV Real-World Example

Working with JSON Files

JSON is great for structured configuration data.

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

Problem: Creating nested directories fails Solution: Use -Force to create parent directories

2. Not Using -Raw When Needed

Problem: Get-Content returns array when you need string Solution: Use -Raw for single string

3. Forgetting -NoTypeInformation in CSV Export

Problem: CSV has type information header Solution: Always use -NoTypeInformation

4. Not Handling Errors in File Operations

Problem: Script stops on permission errors Solution: Use -ErrorAction

Key Takeaways

  • Use Join-Path for cross-platform path handling

  • Test-Path before operations on uncertain paths

  • Get-ChildItem with filters is powerful for file discovery

  • -WhatIf preview changes before destructive operations

  • CSV and JSON are ideal for structured data

  • Select-String is grep for PowerShell

  • -Raw reads files as single string

  • -Recurse processes directory trees

  • Error handling prevents script failures

What You've Learned

✅ Navigating and exploring the file system ✅ Creating, copying, moving, and deleting files and folders ✅ Reading and writing files with various encodings ✅ Searching file content with Select-String ✅ Working with CSV, JSON, and XML files ✅ Real-world file operation patterns ✅ Common pitfalls and how to avoid them

Next Steps

Now that you can work with files and data, it's time to create reusable scripts. In PowerShell Scripting Basics, you'll discover:

  • Creating .ps1 script files

  • Script parameters and validation

  • Execution policies

  • Comment-based help

  • Building production-ready scripts

Let's transform your PowerShell knowledge into reusable automation tools.


Ready to write scripts? Continue to PowerShell Scripting Basics

Last updated