Created
November 2, 2024 04:26
-
-
Save DerfJagged/9928c3c4340e35dda8c423bf3ded481c to your computer and use it in GitHub Desktop.
Rename Windows Profile in Registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to change all registry entries containing old username to new username. | |
# | |
# 1. Create a second admin account on your PC | |
# 2. Log out of your primary account and into the secondary account | |
# 3. Rename profile via Control Panel > User Accounts | |
# 4. Rename old username in C:\Users\ to new username | |
# 5. Change the two variables below and run the script in Admin PowerShell. It will take a long time. | |
# 6. Log into the renamed profile and run the script again. This will change the registry entries that may be only editable by the owner account. | |
# Define the old and new user paths | |
$oldUserPath = "\Users\Jeremy" | |
$newUserPath = "\Users\MisterAnonymous" | |
################################################################################################################################################# | |
# Function to search and replace in registry key values | |
function Replace-RegistryValue { | |
param ( | |
[string]$rootKeyPath | |
) | |
# Get all registry subkeys recursively | |
$subKeys = Get-ChildItem -Path $rootKeyPath -Recurse -ErrorAction SilentlyContinue | |
foreach ($subKey in $subKeys) { | |
# Get all properties (values) under each key | |
$properties = Get-ItemProperty -Path $subKey.PSPath -ErrorAction SilentlyContinue | |
foreach ($property in $properties.PSObject.Properties) { | |
# Check if the value contains the old user path | |
$oldValue = $property.Value | |
if ($oldValue -like "*$oldUserPath*") { | |
$newValue = $property.Value -replace [regex]::Escape($oldUserPath), $newUserPath | |
Set-ItemProperty -Path $subKey.PSPath -Name $property.Name -Value $newValue | |
Write-Host "Updated: $($subKey.PSPath)\$($property.Name)`nFrom $oldValue to $newValue" | |
} | |
} | |
} | |
} | |
# List of registry root keys to search (can be extended based on the need) | |
$registryRoots = @( | |
"HKCU:\", | |
"HKLM:\", | |
"HKCR:\", | |
"HKU:\", | |
"HKCC:\" | |
) | |
# Loop through each root key and apply the search/replace function | |
foreach ($rootKey in $registryRoots) { | |
Write-Host "Searching in $rootKey..." | |
Replace-RegistryValue -rootKeyPath $rootKey | |
} | |
Write-Host "Registry update completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment