← script library

Automated File Organization Script

Files & FoldersJanuary 10, 2025

Keeping files organized is vital for effective data management and productivity. In this post, we will introduce a PowerShell script that automates the process of organizing files in a specified directory based on their file extensions. This script will help you classify files into respective folders, making it easier to locate and manage them.

powershell
$SourceDirectory = "C:\Path\To\Your\Folder"

$Files = Get-ChildItem -Path $SourceDirectory -File
foreach ($File in $Files) {
    $Extension = $File.Extension.TrimStart('.')
    $TargetFolder = Join-Path -Path $SourceDirectory -ChildPath $Extension
    if (-not (Test-Path -Path $TargetFolder)) {
        New-Item -Path $TargetFolder -ItemType Directory | Out-Null
    }
}
Write-Host "Target folders created for each file type."

foreach ($File in $Files) {
    $Extension = $File.Extension.TrimStart('.')
    $TargetFolder = Join-Path -Path $SourceDirectory -ChildPath $Extension
    Move-Item -Path $File.FullName -Destination $TargetFolder
}
Write-Host "Files have been successfully organized into their respective folders."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.