← script library

Efficient File and Folder Management Script

Files & FoldersJanuary 10, 2025

In this post, we introduce a PowerShell script designed for efficient file and folder management. This script automates the process of organizing files by moving them into specified directories based on their types. It's a valuable tool for both system administrators and regular users who want to keep their file systems tidy.

powershell
$sourcePath = 'C:\SourceFolder'
$destinationPathDocuments = 'C:\OrganizedFiles\Documents'
$destinationPathImages = 'C:\OrganizedFiles\Images'
$destinationPathOthers = 'C:\OrganizedFiles\Others'

$destinationPaths = @($destinationPathDocuments, $destinationPathImages, $destinationPathOthers)
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 $destinationPathDocuments
            Write-Host "Moved document: $($_.Name) to $destinationPathDocuments"
            break
        }
        '.jpg', '.png', '.gif' {
            Move-Item -Path $_.FullName -Destination $destinationPathImages
            Write-Host "Moved image: $($_.Name) to $destinationPathImages"
            break
        }
        default {
            Move-Item -Path $_.FullName -Destination $destinationPathOthers
            Write-Host "Moved other file: $($_.Name) to $destinationPathOthers"
            break
        }
    }
}

Write-Host "File organization complete! Summary:"
$destinationPaths | ForEach-Object {
    $fileCount = (Get-ChildItem -Path $_ -File).Count
    Write-Host "$fileCount files moved to $($_)"
}

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.