使用PowerShell一键备份我的文档视频图片等
使用PowerShell一键备份我的文档视频图片等
1.右键开始菜单-终端(管理员),执行下边命令:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
WIN10是右键开始菜单- Windows PowerShell(管理员)
2.开始菜单里搜索“Windows PowerShell ISE”,打开后,新建一个文档,把下边内容复制进去,然后保存,例如:“D:\bb.ps1”
# 使用PowerShell一键备份我的文档视频图片等 # 配置目标磁盘根路径(可修改此处) $targetDrive = "D:" $targetRoot = Join-Path -Path $targetDrive -ChildPath "\" # $targetRoot 的值示例:D:\ # 定义用户目录映射关系 $folderMappings = @{ "Documents" = "我的文档" "Videos" = "我的视频" "Pictures" = "我的图片" "Downloads" = "我的下载" "Music" = "我的音乐" } # 获取当前用户目录,示例:C:\Users\jk,jk是用户名 $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" # 目录说明备份 C:\Users\jk\Downloads 到 D:\我的下载 # 加暂停 只是为了调试 #Write-Host "按回车继续执行" #Pause 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 /R:0 /W:0 /NFL /NDL Write-Host "--已备份 $sourcePath 到 $targetPath" -ForegroundColor Green } } catch { Write-Error "处理 $sourceFolder 时出错: $_" } } Write-Host "--所有备份完成!" -ForegroundColor Green Pause
3.右键bb.ps1 - 使用 PowerShell 执行。
说明:
%USERPROFILE%\Videos
%USERPROFILE%\Documents
%USERPROFILE%\Pictures
%USERPROFILE%\Downloads
%USERPROFILE%\Music

浙公网安备 33010602011771号