ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!
Other languages

Sort your files on desktop

Submitted by technomaster at 10-05-2025, 12:51 PM


Sort your files on desktop
326 Views
#1
Here is a code snippet to organize your desktop.

[ Hidden Content! ]
Here's a simple PowerShell script that organizes all files on your Desktop into folders based on their file types (e.g.,
Code:
.jpg
files go into an "Images" folder,
Code:
.docx
files into "Documents", etc.).How to Use:
  1. Open Notepad and paste the script.
  2. Save it as
    Code:
    Sort-Desktop.ps1
    on your desktop.
  3. Right-click the Start button → select Windows PowerShell (Admin).
  4. Navigate to your desktop using:
    cd $HOME\DesktopRun the script:
    .\Sort-Desktop.ps1 
 
Code:
# Define the path to the Desktop
$desktopPath = [Environment]::GetFolderPath("Desktop")

# Define folder mappings based on file extensions
$fileTypeMap = @{
    "Images"     = @("jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg")
    "Documents"  = @("doc", "docx", "pdf", "txt", "xls", "xlsx", "ppt", "pptx")
    "Videos"     = @("mp4", "avi", "mov", "wmv", "mkv")
    "Music"      = @("mp3", "wav", "flac", "aac")
    "Archives"   = @("zip", "rar", "7z", "tar", "gz")
    "Scripts"    = @("ps1", "bat", "sh", "py", "js")
    "Executables"= @("exe", "msi")
    "Other"      = @()
}

# Get all files on the desktop (not folders)
$files = Get-ChildItem -Path $desktopPath -File

foreach ($file in $files) {
    $extension = $file.Extension.TrimStart(".").ToLower()

    # Find the folder name based on the extension
    $folderName = $fileTypeMap.Keys | Where-Object {
        $fileTypeMap[$_].Contains($extension)
    }

    if (-not $folderName) {
        $folderName = "Other"
    }

    $targetFolder = Join-Path -Path $desktopPath -ChildPath $folderName

    # Create the folder if it doesn't exist
    if (-not (Test-Path $targetFolder)) {
        New-Item -Path $targetFolder -ItemType Directory | Out-Null
    }

    # Move the file
    $destination = Join-Path -Path $targetFolder -ChildPath $file.Name
    Move-Item -Path $file.FullName -Destination $destination
}

Write-Host "Files sorted successfully!"

0
Reply


Messages In This Thread
Sort your files on desktop - by technomaster - 10-05-2025, 12:51 PM
RE: Sort your files on desktop - by FormerSealer - 10-05-2025, 01:50 PM
RE: Sort your files on desktop - by Kukata - 22-05-2025, 12:33 PM


Users browsing this thread: