问题
Trying to make a script to backup specific folders and then rename the GUID key for the target user under HKLM...\ProfileLists but the rename-item command makes a copy of the key and creates a new key with the appended name though having full access
Tried with -force, tried with move-item instead of rename but it gives the exact same results, a new identical key as original but with an appended name
if ((Test-Path $FULLPATH)) {
Rename-Item $FULLPATH -NewName "$SSID.bak" -Force
if ($?) {
Write-Host "$USERNAME was SUCCESSFULLY renamed in the registry"
}
}
Expected result is to only rename the GUID-key in the registry. Actual result is a duplicate key with the new duplicate to have the correct appended name.
Rename-Item : The registry key at the specified path does not exist. At line:9 char:5 + Rename-Item $FULLPATH -NewName "$SSID.bak" -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (HKEY_LOCAL_MACH...\folderredirect:String) [Rename-Item], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Object reference not set to an instance of an object. At line:9 char:5 + Rename-Item $FULLPATH -NewName "$SSID.bak" -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Rename-Item], NullReferenceException + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RenameItemCommand
The key does exist and I can run the same command again as proof (due to the test-path).
Verbose output do confirm its copying
VERBOSE: Performing the operation "Copy Key" on target "the key in question"
回答1:
It works for me. Are your variables set in this way?
$SSID = 'S-1-5-21-1212123708-1212126490-1212120831-1001'
$FULLPATH = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1212123708-1212126490-1212120831-1001'
rename-item $fullpath -newname "$SSID.bak"
回答2:
I was unable to rename the keys with rename-item with full paths and not using variables either. However resolved it by replacing rename-item to
# Rename the Registry profile
if ((Test-Path $FULLPATH)) {
reg copy "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$SSID" "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$SSID-$(Get-Date -f yyy-MM-DD)" /s /f
if (( $? -eq 'True' )) {
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$SSID" /f
}
if (( $? -eq 'True' )) {
Write-Host "$USERNAME was SUCCESSFULLY renamed in the registry"
}
had to use half the full path in the reg copy/delete commands as the syntax is different (no colon). This worked
来源:https://stackoverflow.com/questions/57402478/rename-item-makes-a-copy-with-the-new-name