PowerShell Lesson 2: Files and Folders

📎 This article includes 2 downloadable practice files ↓

⏱ 1 min read

📘 PowerShell Beginner Course · Lesson 2 of 6

“`powershell
$dir = Join-Path $env:TEMP “ps-lesson2”
New-Item -ItemType Directory -Path $dir -Force | Out-Null
Get-ChildItem $dir -Filter *.txt
Get-ChildItem $dir -Filter *.txt | Rename-Item -NewName { $_.Name -replace “report_”, “2026-10_report_” }
Copy-Item “$dir\*.txt” -Destination D:\Backup
Remove-Item $dir -Recurse -Force
“`

  • -Recurse includes subfolders; -File/-Directory filter types.
  • $_ is the current item in the pipeline.
  • -replace uses regular expressions for renaming patterns.
⚠️ Add -WhatIf to Remove-Item, Move-Item and Rename-Item the first time you run a command. It shows what would happen without changing anything.

More: bulk rename files.

Practice

Run the script (it works in a temp folder and cleans up).

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Reply

Your email address will not be published. Required fields are marked *