将vscode的数据从C盘迁移至D盘

张开发
2026/6/10 17:33:00 15 分钟阅读

分享文章

将vscode的数据从C盘迁移至D盘
将下面脚本代码保存成一个ps1文件:migrate-vscode.ps1然后用管理员身份打开powershell然后执行此脚本我的脚本保存在D:\MyDesktop\git\migrate-vscode.ps1,所以这样输入具体得看你自己的保存位置迁移完最好是重启一下电脑。# VS Code 数据迁移脚本 - 从 C 盘迁移到 D 盘# 请先关闭所有 VS Code 窗口再运行此脚本Write-Host-ForegroundColor CyanWrite-Host VS Code 数据迁移C盘 - D盘-ForegroundColor CyanWrite-Host-ForegroundColor CyanWrite-Host# 辅助函数格式化文件大小functionFormat-FileSize{param([long]$Bytes)if($Bytes-ge1TB){return{0:F2} TB-f($Bytes/1TB)}if($Bytes-ge1GB){return{0:F2} GB-f($Bytes/1GB)}if($Bytes-ge1MB){return{0:F2} MB-f($Bytes/1MB)}if($Bytes-ge1KB){return{0:F2} KB-f($Bytes/1KB)}return$BytesB}# 辅助函数计算目录大小functionGet-DirectorySize{param([string]$Path)if(-not(Test-Path$Path)){return0}$size(Get-ChildItem-Path$Path-Recurse-Force-ErrorAction SilentlyContinue|Where-Object{-not$_.PSIsContainer}|Measure-Object-Property Length-Sum).Sumreturn$size}# 累计释放空间$freedBytes 0# 检查 VS Code 是否已关闭$vscodeRunningGet-ProcessCode-ErrorAction SilentlyContinueif($vscodeRunning){Write-Host[错误] 检测到 VS Code 仍在运行请关闭所有 VS Code 窗口后重试。-ForegroundColor RedWrite-Host按任意键退出...$null$Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown)exit1}Write-Host[1/3] 迁移 .vscode (扩展/插件)...-ForegroundColor Yellow$src$env:USERPROFILE\.vscode$dstD:\VSCodeData\.vscodeif((Get-Item$src-Force).Attributes-matchReparsePoint){Write-Host 已是 Junction跳过-ForegroundColor Green}else{Write-Host 正在计算目录大小...$size1Get-DirectorySize$srcWrite-Host 当前大小:$(Format-FileSize$size1)if(Test-Path$dst){Write-Host 目标已存在先删除...Remove-Item$dst-Recurse-Force}Write-Host 正在移动数据到 D 盘 (可能需要一两分钟)...Move-Item-Path$src-Destination$dst-ForceWrite-Host 创建 Junction...cmd/c mklink/J$src$dst$freedBytes$size1Write-Host .vscode 迁移完成! 释放$(Format-FileSize$size1)-ForegroundColor Green}Write-HostWrite-Host[2/3] 迁移 workspaceStorage...-ForegroundColor Yellow$src$env:APPDATA\Code\User\workspaceStorage$dstD:\VSCodeData\workspaceStorageif((Get-Item$src-Force).Attributes-matchReparsePoint){Write-Host 已是 Junction跳过-ForegroundColor Green}else{# 计算 C 盘原目录大小$size2Get-DirectorySize$srcWrite-Host 当前大小:$(Format-FileSize$size2)# 删除 C 盘残留如果存在if(Test-Path$src){Remove-Item$src-Recurse-Force-ErrorAction SilentlyContinue}# 确保 D 盘目标存在if(-not(Test-Path$dst)){New-Item-ItemType Directory-Path$dst-Force|Out-Null}Write-Host 创建 Junction...cmd/c mklink/J$src$dst$freedBytes$size2Write-Host workspaceStorage 迁移完成! 释放$(Format-FileSize$size2)-ForegroundColor Green}Write-HostWrite-Host[3/3] 迁移 CachedData...-ForegroundColor Yellow$src$env:APPDATA\Code\CachedData$dstD:\VSCodeData\CachedDataif((Get-Item$src-Force).Attributes-matchReparsePoint){Write-Host 已是 Junction跳过-ForegroundColor Green}else{Write-Host 正在计算目录大小...$size3Get-DirectorySize$srcWrite-Host 当前大小:$(Format-FileSize$size3)if(Test-Path$dst){Write-Host 目标已存在先删除...Remove-Item$dst-Recurse-Force}Write-Host 正在移动数据到 D 盘 (可能需要一两分钟)...Move-Item-Path$src-Destination$dst-ForceWrite-Host 创建 Junction...cmd/c mklink/J$src$dst$freedBytes$size3Write-Host CachedData 迁移完成! 释放$(Format-FileSize$size3)-ForegroundColor Green}Write-HostWrite-Host-ForegroundColor Greenif($freedBytes-gt0){Write-Host 迁移完成已释放约$(Format-FileSize$freedBytes)C 盘空间-ForegroundColor Green}else{Write-Host 迁移完成所有数据已在 D 盘无需重复迁移-ForegroundColor Green}Write-Host-ForegroundColor GreenWrite-HostWrite-Host验证结果:-ForegroundColor Cyan$paths ($env:USERPROFILE\.vscode,$env:APPDATA\Code\User\workspaceStorage,$env:APPDATA\Code\CachedData)foreach($pin$paths){$isJunction(Get-Item$p-Force).Attributes-matchReparsePoint$markif($isJunction){[JUNCTION]}else{[普通目录]}Write-Host$mark$p}Write-HostWrite-Host按任意键退出...$null$Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown)

更多文章