runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

将我的文档、图片、视频等目录迁移到D盘 runliuv

 

注意:

这个脚本只适用于:从未做过映射迁移的电脑。

因为“我的文档”变量是写死的:[Environment]::GetFolderPath("UserProfile") + Documents = C:\Users\jk\Documents。

 

1.右键开始菜单-终端(管理员),执行下边命令:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

WIN10是右键开始菜单- Windows PowerShell(管理员)

2.开始菜单里搜索“Windows PowerShell ISE”,打开后,新建一个文档,把下边内容复制进去,然后保存,例如:“D:\aa.ps1”

# 配置目标磁盘根路径(可修改此处)
$targetDrive = "D:"
$targetRoot = Join-Path -Path $targetDrive -ChildPath "\"

# 定义用户目录映射关系

$folderMappings = @{
    "Documents"       = "我的文档"
    "Videos"       = "我的视频"
    "Pictures"       = "我的图片"    
    "Downloads"       = "我的下载"
    "Music"       = "我的音乐"
}

# 获取当前用户目录
$userProfile = [Environment]::GetFolderPath("UserProfile")

# 先把各文件夹内容移动到目标目录,然后再更改映射
Write-Host "--  准备移动文件夹 --"
foreach ($sourceFolder in $folderMappings.Keys) {
    $targetFolderName = $folderMappings[$sourceFolder]
    # C:\Users\jk\Documents
    $sourcePath = Join-Path -Path $userProfile -ChildPath $sourceFolder
    Write-Host "--  源目录: $sourcePath"
    # D:\我的文档
    $targetPath = Join-Path -Path $targetRoot -ChildPath $targetFolderName
    Write-Host "--目标目录: $targetPath"
     

    try {
        # 创建目标文件夹(如果不存在)
        if (-not (Test-Path -Path $targetPath)) {
            New-Item -Path $targetPath -ItemType Directory -Force | Out-Null
            Write-Host "已创建目标文件夹: $targetPath"
        }
         
        # 移动文件(如果源文件夹存在)
        if (Test-Path -Path $sourcePath) {
            # 使用robocopy确保移动所有内容(包括隐藏文件)
            Write-Host "准备把 $sourcePath 目录中的内容移动到 $targetPath"
          #  Write-Host "按回车继续执行"
          #  Pause
            robocopy $sourcePath $targetPath /E /MOVE /R:0 /W:0 /NFL /NDL
            Write-Host "已移动 $sourcePath 到 $targetPath"
        }       
    }
    catch {
        Write-Error "处理 $sourceFolder 时出错: $_"
    }
}

# 最后在更改映射
Write-Host "--  准备更改映射 --"
foreach ($sourceFolder in $folderMappings.Keys) {
    $targetFolderName = $folderMappings[$sourceFolder]
    $sourcePath = Join-Path -Path $userProfile -ChildPath $sourceFolder
    Write-Host "源目录: $sourcePath"
    $targetPath = Join-Path -Path $targetRoot -ChildPath $targetFolderName
    Write-Host "目标目录: $targetPath"     

    try {        
         
        # 设置文件夹重定向(通过注册表永久修改)
        $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
        $folderKey = switch ($sourceFolder) {
            "Videos" { "My Video" }
            "Pictures" { "My Pictures" }
            "Documents" { "Personal" }
            "Downloads" { "{374DE290-123F-4565-9164-39C4925E467B}" }
            "Music" { "My Music" }
        }

        if ($folderKey) {
            Set-ItemProperty -Path $regPath -Name $folderKey -Value $targetPath -Type ExpandString
            Write-Host "已设置注册表重定向: $sourceFolder -> $targetPath"
        }
    }
    catch {
        Write-Error "处理 $sourceFolder 时出错: $_"
    }
}

# 刷新资源管理器使设置生效
Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Start-Process explorer.exe

Write-Host "所有操作完成!请重启电脑以确保设置完全生效。" -ForegroundColor Green
Pause

 

3.右键aa.ps1 - 使用 PowerShell 执行。

 

注意:

这个脚本只适用于:从未做过映射迁移的电脑。

因为“我的文档”变量是写死的:[Environment]::GetFolderPath("UserProfile") + Documents = C:\Users\jk\Documents。

 

posted on 2025-12-01 16:59  runliuv  阅读(11)  评论(0)    收藏  举报