Organize Your Downloads Folder with a Simple PowerShell Script

3 minute read

Published:

Tired of a cluttered Downloads folder? This post will show you how to use a simple PowerShell script to automatically sort your files into categories, keeping your folder neat and tidy. The script is from the PowerShell-Download-Organizer repository, is easy to set up, and can be automated with Windows Task Scheduler.

The Script

Here is the full PowerShell script from the repository. You can save it as organize-downloads.ps1.

# Specify the target folder to organize
$targetFolder = "C:\Users\shiva\Downloads"

# Define the categories and their corresponding file extensions
$categories = @{
    "Images"      = @(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".webp",
".tiff", ".ico");
    "Videos"      = @(".mp4", ".mov", ".avi", ".mkv", ".wmv", ".flv", ".webm", "
.m4v");
    "Audio"       = @(".mp3", ".wav", ".aac", ".ogg", ".flac", ".wma", ".m4a");
    "Documents"   = @(".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
 ".txt", ".rtf", ".csv", ".epub");
    "Archives"    = @(".zip", ".rar", ".7z", ".tar", ".gz", ".pkg");
    "Executables" = @(".exe", ".msi", ".bat", ".sh", ".jar");
}

# Get all files in the target folder, excluding directories
$files = Get-ChildItem -Path $targetFolder -File

foreach ($file in $files) {
    # Get the file's extension in lowercase
    $extension = $file.Extension.ToLower()
    $destinationFolder = ""

    # Find which category the file belongs to
    foreach ($category in $categories.Keys) {
        if ($categories[$category] -contains $extension) {
            $destinationFolder = Join-Path -Path $targetFolder -ChildPath $categ
ory
            break # Stop searching once a match is found
        }
    }

    # If no category was found, place it in the "Other" folder
    if ([string]::IsNullOrWhiteSpace($destinationFolder)) {
        $destinationFolder = Join-Path -Path $targetFolder -ChildPath "Other"
    }

    # Create the destination folder if it doesn't exist
    if (-not (Test-Path -Path $destinationFolder)) {
        New-Item -Path $destinationFolder -ItemType Directory
    }

    # Move the file to the correct category folder
    Move-Item -Path $file.FullName -Destination $destinationFolder
}

How to Use the Script

1. Download and Customize the Script

  1. Download the script file: organize-downloads.ps1 from the repository.
  2. Move the file to a permanent location on your computer (e.g., your Documents folder).
  3. Right-click the script file and choose Edit to open it with Notepad or another text editor.
  4. Important: Change the folder path on line 2 to the folder you want to organize.

    # Change this path to your own Downloads folder or any folder you want to organize
    $targetFolder = "C:\Users\YourUsername\Downloads"
    

2. Set the PowerShell Execution Policy

By default, Windows prevents scripts from running for security. You need to run a one-time command to allow your own scripts to run.

  1. Open PowerShell as an Administrator (search for PowerShell in the Start Menu, right-click it, and select Run as administrator).
  2. Copy and paste the following command into the window, then press Enter:
    Set-ExecutionPolicy RemoteSigned
    
  3. When prompted, type Y and press Enter to confirm. You only have to do this once.

3. Automate with Task Scheduler

  1. Press the Windows Key, type Task Scheduler, and press Enter.
  2. In the right-hand “Actions” pane, click Create Task….
  3. General Tab:
    • Name: Give it a name like “Organize Downloads Folder”.
    • Select “Run whether user is logged on or not”.
  4. Triggers Tab:
    • Click New….
    • Choose a schedule that works for you. A simple Daily trigger is recommended.
    • Click OK.
  5. Actions Tab:
    • Click New….
    • Program/script: powershell.exe
    • Add arguments (optional): -File "C:\Path\To\Your\organize-downloads.ps1"
      • (Make sure to replace the path with the actual full path to where you saved the script!)
    • Click OK.
  6. Click OK again to save the task. You may be asked to enter your Windows password.

That’s it! The script will now run automatically on your schedule.

How to Customize Categories

You can easily change which file extensions go into which folders by editing the $categories list at the top of the organize-downloads.ps1 script.

# Define the categories and their corresponding file extensions
$categories = @{
    "Images"      = @(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".webp");
    "Videos"      = @(".mp4", ".mov", ".avi", ".mkv", ".wmv");
    "Documents"   = @(".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt");
    # Add or remove extensions here
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...