In this post, we share a PowerShell script that automates the process of sorting files based on their types and moves them to designated folders. This script is essential for keeping your workspace organized and efficient.
powershell
$sourcePath = 'C:\FilesToSort'
$destinationPathDocs = 'C:\SortedFiles\Documents'
$destinationPathImages = 'C:\SortedFiles\Images'
$destinationPathAudio = 'C:\SortedFiles\Audio'
$destinationPaths = @($destinationPathDocs, $destinationPathImages, $destinationPathAudio)
foreach ($path in $destinationPaths) {
if (-Not (Test-Path -Path $path)) {
New-Item -ItemType Directory -Path $path
Write-Host "Created directory: $path"
}
}
Get-ChildItem -Path $sourcePath -File | ForEach-Object {
switch -Wildcard ($_.Extension) {
'.pdf', '.docx', '.txt' {
Move-Item -Path $_.FullName -Destination $destinationPathDocs
Write-Host "Moved document: $($_.Name) to $destinationPathDocs"
break
}
'.jpg', '.png', '.gif' {
Move-Item -Path $_.FullName -Destination $destinationPathImages
Write-Host "Moved image: $($_.Name) to $destinationPathImages"
break
}
'.mp3', '.wav' {
Move-Item -Path $_.FullName -Destination $destinationPathAudio
Write-Host "Moved audio file: $($_.Name) to $destinationPathAudio"
break
}
default {
Write-Host "Unsupported file type: $($_.Name)"
break
}
}
}
Write-Host "File sorting completed. Check the destination directories for organized files."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.