1

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"
Theo
  • 57,719
  • 8
  • 24
  • 41
  • 4
    IMO the small code fragment/details you provided isn't sufficient to give any hints. –  Aug 07 '19 at 21:41
  • It may be you need `Rename-ItemProperty` - more (relevant) details help us help you. – G42 Aug 07 '19 at 22:08
  • It's always worth striving for an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve). – mklement0 Aug 07 '19 at 22:42

2 Answers2

0

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"
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Yes, sort of - `$USERNAME = read-host "Enter Username" $SID = Get-WmiObject Win32_UserAccount -Filter "Name = '$USERNAME'" $SSID = $SID.SID $REGPATH = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $FULLPATH = "$REGPATH\$SSID" ` – user3415995 Aug 08 '19 at 13:31
  • @user3415995 The error message suggests otherwise though.. `$REGPATH` should either start with `HKLM:` or `Registry::HKEY_LOCAL_MACHINE` (alternative path that begins with the provider name followed by **two** colons) – Theo Aug 08 '19 at 14:16
  • You might put all that in the question. It seems like it should work unless something went wrong. – js2010 Aug 08 '19 at 14:32
0

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