You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写PowerShell脚本实现桌面文件自动分类整理?

PowerShell桌面文件自动整理脚本

以下是可直接运行的PowerShell脚本,支持自定义扩展名分类,能自动将桌面文件按类型移动到对应文件夹:

# 自定义扩展名-文件夹映射,可根据需求修改/添加
$folderMappings = @{
    "音频文件" = @(".mp3", ".wav", ".flac", ".m4a")
    "图片文件" = @(".jpg", ".jpeg", ".png", ".gif", ".bmp")
    "文本文件" = @(".txt", ".md", ".docx", ".pdf", ".xlsx")
    "其他文件" = @("*") # 匹配所有未被上面分类的文件
}

# 获取当前用户桌面路径
$desktopPath = [Environment]::GetFolderPath('Desktop')

# 遍历每个分类规则
foreach ($category in $folderMappings.Keys) {
    $targetFolder = Join-Path -Path $desktopPath -ChildPath $category
    # 若目标文件夹不存在则创建
    if (-not (Test-Path -Path $targetFolder)) {
        New-Item -Path $targetFolder -ItemType Directory | Out-Null
    }

    $extensions = $folderMappings[$category]
    # 查找桌面中对应扩展名的文件(仅根目录,不包含子文件夹)
    foreach ($ext in $extensions) {
        $files = Get-ChildItem -Path $desktopPath -Filter $ext -File
        foreach ($file in $files) {
            $targetPath = Join-Path -Path $targetFolder -ChildPath $file.Name
            # 处理重名文件,添加数字后缀避免覆盖
            $counter = 1
            while (Test-Path -Path $targetPath) {
                $newFileName = "{0}_{1}{2}" -f $file.BaseName, $counter, $file.Extension
                $targetPath = Join-Path -Path $targetFolder -ChildPath $newFileName
                $counter++
            }
            # 移动文件
            Move-Item -Path $file.FullName -Destination $targetPath -Force
        }
    }
}

自定义配置说明

  • 修改$folderMappings中的键值对即可调整分类规则:
    • 键为目标文件夹名称(会在桌面自动创建)
    • 值为对应扩展名的数组,支持通配符*(如"其他文件"分类)
  • 可添加新分类,例如:"视频文件" = @(".mp4", ".avi", ".mov")

设置每周五自动运行

通过Windows任务计划程序实现定期执行:

  1. 打开「任务计划程序」,点击「创建基本任务」
  2. 任务名称设为「桌面文件自动整理」,描述按需填写
  3. 触发器选择「每周」,设置为每周五,指定执行时间(如18:00)
  4. 操作选择「启动程序」,程序/脚本填powershell.exe,添加参数填-WindowStyle Hidden -File "你的脚本保存路径\DesktopCleanup.ps1"
  5. 完成向导,确保任务已启用

内容的提问来源于stack exchange,提问作者Nico Sören

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 06:47:05