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.,
files go into an "Images" folder,
files into "Documents", etc.).How to Use:
Code:
.jpg
Code:
.docx
- Open Notepad and paste the script.
- Save it as on your desktop.Code:
Sort-Desktop.ps1
- Right-click the Start button → select Windows PowerShell (Admin).
- 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!"