gpt4 book ai didi

Powershell查找并替换注册表值?

转载 作者:行者123 更新时间:2023-12-04 16:38:28 25 4
gpt4 key购买 nike

'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' 我将一些路径设置为旧服务器。例如:

“我的图片”设置为 '\\DeadServer\RedirectedFolders\%UserName%\My Documents\My Pictures'

我想将 "\\DeadServer\RedirectedFolders" 替换为 "C:\Users"

如何在 powershell 中做到这一点?


我尽力了

Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" | ? {$_.PSObject.Properties -like "*DeadServer*"} 

但我想我对我想要更改的条目是“属性”而不是“项目”感到困惑,而且我不知道如何像做项目那样遍历属性。


在您问之前,我已经使用组策略进行了此更改,但它没有采取。用户收到消息

The Recycle Bin on \DeadServer\RedirectedFolders\%UserName%\My Documents\My Pictures` is corrupted. Do you want to empty the Recycle Bin for this drive?

在登录时阻止应用文件夹重定向。
这是我尝试手动强制更改回本地存储。

最佳答案

我想通了。花了我很长时间,但我写了一个相当不雅的脚本:

Get-Item -ErrorAction SilentlyContinue -path  "Microsoft.PowerShell.Core\Registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" |
foreach {
Get-ItemProperty -Path "Microsoft.PowerShell.Core\Registry::$_" |
foreach {
$CurrentUserShellFoldersPath = $_.PSPath
$SID = $CurrentUserShellFoldersPath.Split('\')[2]
$_.PSObject.Properties |
foreach {
if ($_.Value -like "*DeadServer*") {
write-host "Path:`t`t"$CurrentUserShellFoldersPath
write-host "SID:`t`t"$SID
write-host "Name:`t`t"$_.Name
write-host "Old Value:`t"$_.Value
$newValue = $_.Value
$newValue = $newValue -replace '\\\\DeadServer\\RedirectedFolders', "C:\Users"
$newValue = $newValue -replace "My Documents\\", ""
$newValue = $newValue -replace "My ", ""
Write-Host "New Value:`t"$newValue
Set-ItemProperty -Path $CurrentUserShellFoldersPath -Name $_.Name -Value $newValue

Write-host "================================================================"
}
}
}
}

如果你们有的话,我很想知道一种更快或更优雅的方法来做到这一点。

关于Powershell查找并替换注册表值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com