实际运维中用户配置和outlook默认OST文件路径在C盘,经常会出现C盘空间不足,系统奔溃或者什么其问题导致无法访问在C盘下的桌面,文档,收藏夹等资料。手动剪切配置和配置OST路径 一台两台都还好,如果电脑有成千上万台,那人都会操作傻了,
新发现文件可以显示中文或者图标的关键点是当初在这里卡的许久
一定要复制desktop.ini配置文件到新文件夹且新文件夹是系统属性,可以使用attrib +s 命令添加
脚本运行的效果图如下
powershell 修改用户配置文件路径,outlook默认OST文件路径
代码如下,迁移过程请关闭当前打开的所有程序,尤其是用户配置文件下文件


$olduserfiles=([Environment]::GetFolderPath("Desktop") -split "\\desktop")[0] #当前配置路径
#磁盘状态
Get-WmiObject Win32_Volume |Where-Object{$_.DriveLetter} | Sort-Object DriveLetter | Format-Table @{NAME="盘符";express={$_.DriveLetter}},@{name="总容量";express={"{0:N2}" -f ($_.Capacity/1GB)+"G"}},@{name="剩余容量";express={"{0:N2}" -f ($_.FreeSpace/1GB)+"G"}}
Write-Output  "当前配置文件路劲:$olduserfiles"
do {
    $volume= Read-Host "选择迁移到的盘符"
    $volume=$volume.Trim().ToUpper()+":"                               
} while( !(Test-Path $volume))
$ostpath=$volume+"\users\mail-ost"
#oultook OST默认缓存路劲
if (!(Test-Path $ostpath)) {mkdir $ostpath}

if ((Test-Path "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE") -or (Test-Path "C:\Program Files\Microsoft Office\Office14\outlook.exe")){
    reg add "hkcu\Software\Microsoft\Office\14.0\Outlook" /v ForceOSTPath /t REG_EXPAND_SZ /D $ostpath /F   
}
if ((Test-Path "C:\Program Files (x86)\Microsoft Office\Office15\OUTLOOK.EXE") -or (Test-Path "C:\Program Files\Microsoft Office\Office15\outlook.exe")){
    reg add "hkcu\Software\Microsoft\Office\15.0\Outlook" /v ForceOSTPath /t REG_EXPAND_SZ /D $ostpath /F   
}
if ((Test-Path "C:\Program Files (x86)\Microsoft Office\Office16\OUTLOOK.EXE") -or (Test-Path "C:\Program Files\Microsoft Office\Office16\outlook.exe")){
    reg add "hkcu\Software\Microsoft\Office\16.0\Outlook" /v ForceOSTPath /t REG_EXPAND_SZ /D $ostpath /F   
}
$newuserfiles=$volume+$env:HOMEPATH
if (!(Test-Path $newuserfiles)) {mkdir $newuserfiles}
#复制资料到,新路径
xcopy.exe  $olduserfiles $newuserfiles /y /s /e 
#复制配置desktop.ini到对应新路径
move-Item "$olduserfiles\desktop\desktop.ini" "$newuserfiles\desktop" -Force
move-Item "$olduserfiles\Music\desktop.ini" "$newuserfiles\Music" -Force
move-Item "$olduserfiles\Downloads\desktop.ini" "$newuserfiles\Downloads" -Force
move-Item "$olduserfiles\Documents\desktop.ini" "$newuserfiles\Documents" -Force
move-Item "$olduserfiles\Pictures\desktop.ini" "$newuserfiles\Pictures" -Force
move-Item "$olduserfiles\Searches\desktop.ini" "$newuserfiles\Searches" -Force
move-Item "$olduserfiles\Favorites\desktop.ini" "$newuserfiles\Favorites" -Force
move-Item "$olduserfiles\Videos\desktop.ini" "$newuserfiles\Videos" -Force
move-Item "$olduserfiles\Links\desktop.ini" "$newuserfiles\Links" -Force
move-Item "$olduserfiles\Contacts\desktop.ini" "$newuserfiles\Contacts" -Force
move-Item "$olduserfiles\Saved Games\desktop.ini" "$newuserfiles\Saved Games" -Force
move-Item "$olduserfiles\3D Objects\desktop.ini" "$newuserfiles\3D Objects" -ErrorAction SilentlyContinue -Force
#遍历新路劲的文件夹改成系统文件
Get-ChildItem -Path $newuserfiles | Where-Object {$_.Mode -like "d*"} |ForEach-Object{attrib +s $_.fullname}
#删除旧文件
Get-ChildItem -Path $olduserfiles | Where-Object {$_.Mode -like "d*"} |ForEach-Object{cmd /c rd $_.fullname /q /s}
Get-ChildItem -Path $olduserfiles | Where-Object {$_.Mode -like "d*"} |ForEach-Object{cmd /c rd $_.fullname /q /s}
#修改注册表 
$RegPath = "Hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Set-ItemProperty -PATH $RegPath -Name "{374DE290-123F-4565-9164-39C4925E467B}" -Value $newuserfiles"\Downloads"
Set-ItemProperty -PATH $RegPath -Name "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}" -Value $newuserfiles"\Saved Games"
Set-ItemProperty -PATH $RegPath -Name "{56784854-C6CB-462B-8169-88E350ACB882}" -Value $newuserfiles"\Contacts"
Set-ItemProperty -PATH $RegPath -Name "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}" -Value $newuserfiles"\Searches"
Set-ItemProperty -PATH $RegPath -Name "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}" -Value $newuserfiles"\Links"
Set-ItemProperty -PATH $RegPath -Name "Desktop" -Value $newuserfiles"\Desktop"
Set-ItemProperty -PATH $RegPath -Name "Favorites" -Value $newuserfiles"\Favorites"
Set-ItemProperty -PATH $RegPath -Name "My Music" -Value $newuserfiles"\Music"
Set-ItemProperty -PATH $RegPath -Name "My Pictures" -Value $newuserfiles"\Pictures"
Set-ItemProperty -PATH $RegPath -Name "My Videos" -Value $newuserfiles"\Videos"
Set-ItemProperty -PATH $RegPath -Name "Personal" -Value $newuserfiles"\Documents"

$RegPath = "Hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
Set-ItemProperty -PATH $RegPath -Name "{0DDD015D-B06C-45D5-8C4C-F59713854639}" -Value $newuserfiles"\Pictures"
Set-ItemProperty -PATH $RegPath -Name "{35286A68-3C57-41A1-BBB1-0EAE73D76C95}" -Value $newuserfiles"\Videos"
Set-ItemProperty -PATH $RegPath -Name "{374DE290-123F-4565-9164-39C4925E467B}" -Value $newuserfiles"\Downloads"
Set-ItemProperty -PATH $RegPath -Name "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}" -Value $newuserfiles"\Saved Games"
Set-ItemProperty -PATH $RegPath -Name "{56784854-C6CB-462B-8169-88E350ACB882}" -Value $newuserfiles"\Contacts"
Set-ItemProperty -PATH $RegPath -Name "{754AC886-DF64-4CBA-86B5-F7FBF4FBCEF5}" -Value $newuserfiles"\Desktop"
Set-ItemProperty -PATH $RegPath -Name "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}" -Value $newuserfiles"\Searches"
Set-ItemProperty -PATH $RegPath -Name "{7D83EE9B-2244-4E70-B1F5-5393042AF1E4}" -Value $newuserfiles"\Downloads"
Set-ItemProperty -PATH $RegPath -Name "{A0C69A99-21C8-4671-8703-7934162FCF1D}" -Value $newuserfiles"\Music"
Set-ItemProperty -PATH $RegPath -Name "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}" -Value $newuserfiles"\Links"
Set-ItemProperty -PATH $RegPath -Name "{F42EE2D3-909F-4907-8871-4C22FC0BF756}" -Value $newuserfiles"\Documents"
Set-ItemProperty -PATH $RegPath -Name "Desktop" -Value $newuserfiles"\Desktop"
Set-ItemProperty -PATH $RegPath -Name "Favorites" -Value $newuserfiles"\Favorites"
Set-ItemProperty -PATH $RegPath -Name "My Music" -Value $newuserfiles"\Music"
Set-ItemProperty -PATH $RegPath -Name "My Pictures" -Value $newuserfiles"\Pictures"
Set-ItemProperty -PATH $RegPath -Name "My Videos" -Value $newuserfiles"\Videos"
Set-ItemProperty -PATH $RegPath -Name "Personal" -Value $newuserfiles"\Documents"
#刷新
Stop-Process -Name Explorer -f -ErrorAction SilentlyContinue | Out-Null