在'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 *”}
但是我觉得我对我想要改变的条目是一个'属性'而不是一个'Item'感到困惑,而且我不知道如何遍历像我做项目的属性。
在您提出要求之前,我已经使用组策略进行了此更改,但它没有采取。用户收到消息
\ DeadServer \ RedirectedFolders \%UserName%\ My Documents \ My Pictures`上的回收站已损坏。您要清空此驱动器的回收站吗?
登录时保持文件夹重定向不被应用。 这是我尝试手动强制更改回本地存储。
发布于 2018-10-23 16:00:55
我想到了。花了我很长时间,但我写了一个相当不优雅的剧本:
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 "================================================================"
}
}
}
}
如果你们中的任何一个人,我会喜欢学习更快或更优雅的方法。
发布于 2018-10-23 17:05:18
对此并不满意,所以如果有人对此感到羞耻,我会感到高兴和悲伤。它经过大多数测试,只要验证它是否找到了正确的密钥。
If(!(Test-Path HKU:)){New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS}
$registrySearchPath = "HKU:\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$pathToReplace = [regex]::Escape("C:\Users")
$newPath = '%USERPROFILE%'
Get-Item -path $registrySearchPath -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Name |
Where-Object{$_ -match "^HKEY_USERS\\S-1-5-21"} |
ForEach-Object{
$key = $_ -replace "^HKEY_USERS","HKU:"
(Get-ItemProperty $key).psobject.Properties | Where-Object{$_.Value -match $pathToReplace} |
Select-Object Name,Value | ForEach-Object{
Set-ItemProperty -Path $key -Name $_.Name -Value ($_.Value -replace $pathToReplace,$newPath) -WhatIf
}
}
使用Psdrive映射HKU,因为它不是PowerShell中的默认驱动器。获取至少具有“\ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ User Shell Folders”路径的所有密钥。通过仅查找具有“S-1-5-21”作为密钥一部分的那些来省略默认密钥和任何其他区域设置帐户。然后,对于每个找到的位置,找到每个注册表值,其数据与您要查找的路径匹配。
Set-ItemProperty -Path $key -Name $_.Name -Value ($_.Value -replace $pathToReplace,$newPath) -WhatIf
在这里稍微关注最后一部分。使用匹配的所有值,我们用简单的数据替换数据-replace
。我有一个-WhatIf
确保你测试以防万一发生不好的事情。我建议评论该行并输出只是$_.Value -replace $pathToReplace,$newPath
为了验证它是否正在按照您的预期进行操作。
确保更改值$pathToReplace
,$newPath
然后测试两次,执行一次。
https://stackoverflow.com/questions/-100008913
复制相似问题