🎓 A1Learner
🔍
0 of 52 lessons completed

PowerShell Project: File Organizer

A file organizer sorts files into folders by their type. It moves each file to the folder that matches its ending.

List only the text files

The script creates a folder with two text files. Get-ChildItem with a filter lists only those files, in order.

$base = Join-Path $PSScriptRoot "files"
$null = New-Item -Path $base -ItemType Directory
Set-Content -Path (Join-Path $base "notes.txt") -Value "buy milk"
Set-Content -Path (Join-Path $base "todo.txt") -Value "call Ana"
Get-ChildItem -Path $base -Filter "*.txt" -File | Sort-Object Name | Select-Object -ExpandProperty Name

Move one file into its folder

The script creates a folder for text files. Move-Item moves the text file into that folder.

$base = Join-Path $PSScriptRoot "files"
$null = New-Item -Path $base -ItemType Directory
$textFolder = Join-Path $base "txt"
$null = New-Item -Path $textFolder -ItemType Directory
Set-Content -Path (Join-Path $base "notes.txt") -Value "buy milk"
Move-Item -Path (Join-Path $base "notes.txt") -Destination $textFolder
Get-ChildItem -Path $textFolder -File | Sort-Object Name | Select-Object -ExpandProperty Name

Count the sorted files

The script sorts two text files and one markdown file. Each folder Count shows how many files landed there.

$base = Join-Path $PSScriptRoot "files"
$null = New-Item -Path $base -ItemType Directory
$textFolder = Join-Path $base "txt"
$null = New-Item -Path $textFolder -ItemType Directory
$mdFolder = Join-Path $base "md"
$null = New-Item -Path $mdFolder -ItemType Directory
Set-Content -Path (Join-Path $base "notes.txt") -Value "buy milk"
Set-Content -Path (Join-Path $base "report.txt") -Value "done"
Set-Content -Path (Join-Path $base "readme.md") -Value "hello"
Move-Item -Path (Join-Path $base "notes.txt") -Destination $textFolder
Move-Item -Path (Join-Path $base "report.txt") -Destination $textFolder
Move-Item -Path (Join-Path $base "readme.md") -Destination $mdFolder
"Text files: $((Get-ChildItem -Path $textFolder -File).Count)"
"Markdown files: $((Get-ChildItem -Path $mdFolder -File).Count)"

Example

$base = Join-Path $PSScriptRoot "organizer"
$null = New-Item -Path $base -ItemType Directory
$textFolder = Join-Path $base "txt"
$null = New-Item -Path $textFolder -ItemType Directory
$mdFolder = Join-Path $base "md"
$null = New-Item -Path $mdFolder -ItemType Directory
Set-Content -Path (Join-Path $base "notes.txt") -Value "buy milk"
Set-Content -Path (Join-Path $base "report.txt") -Value "done"
Set-Content -Path (Join-Path $base "readme.md") -Value "hello"
Move-Item -Path (Join-Path $base "notes.txt") -Destination $textFolder
Move-Item -Path (Join-Path $base "report.txt") -Destination $textFolder
Move-Item -Path (Join-Path $base "readme.md") -Destination $mdFolder
"Text files:"
Get-ChildItem -Path $textFolder -File | Sort-Object Name | Select-Object -ExpandProperty Name
"Markdown files:"
Get-ChildItem -Path $mdFolder -File | Sort-Object Name | Select-Object -ExpandProperty Name

Key points

  • Organizer: a script that sorts files into folders by file type.
  • Filter: Get-ChildItem with -Filter picks only files with one ending.
  • Folder: New-Item creates a folder when -ItemType is Directory.
  • Move-Item: a command that moves a file to a new folder.
  • Sorted list: Sort-Object Name lists file names in order.

In this example, the script creates a working folder and two sorted folders. It creates two text files and one markdown file. It moves each file into its matching folder. It prints the sorted names in each folder.

Note

the organizer creates every file and folder it works with, so it runs the same anywhere.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command moves a file into a new folder?