Advertisement
SwarupSaha

Windows Username Change

Mar 24th, 2025
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  # Save this file as: Rename-WindowsUser.ps1
  2. # Run from another admin user account (e.g. tempadmin)
  3.  
  4. param (
  5.     [string]$OldUserName = "swaru",
  6.     [string]$NewUserName = "Swarup"
  7. )
  8.  
  9. Write-Host "Renaming Windows user folder..." -ForegroundColor Cyan
  10.  
  11. # Define paths
  12. $oldPath = "C:\Users\$OldUserName"
  13. $newPath = "C:\Users\$NewUserName"
  14.  
  15. # Check if old folder exists
  16. if (-Not (Test-Path $oldPath)) {
  17.     Write-Host "❌ Old user folder not found: $oldPath" -ForegroundColor Red
  18.     exit
  19. }
  20.  
  21. # Rename folder
  22. try {
  23.     Rename-Item -Path $oldPath -NewName $NewUserName
  24.     Write-Host "✅ Folder renamed: $oldPath → $newPath" -ForegroundColor Green
  25. } catch {
  26.     Write-Host "❌ Failed to rename folder. Make sure you're not logged into that user." -ForegroundColor Red
  27.     exit
  28. }
  29.  
  30. # Modify registry path
  31. Write-Host "Updating Registry path..." -ForegroundColor Cyan
  32.  
  33. # Get list of all profiles in registry
  34. $regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
  35. $profiles = Get-ChildItem $regPath
  36.  
  37. foreach ($profile in $profiles) {
  38.     $profileImagePath = Get-ItemProperty -Path $profile.PSPath | Select-Object -ExpandProperty ProfileImagePath -ErrorAction SilentlyContinue
  39.     if ($profileImagePath -eq $oldPath) {
  40.         Set-ItemProperty -Path $profile.PSPath -Name ProfileImagePath -Value $newPath
  41.         Write-Host "✅ Registry updated for SID: $($profile.PSChildName)" -ForegroundColor Green
  42.     }
  43. }
  44.  
  45. Write-Host "`n🎉 DONE! You can now log in with your original account and see the new path." -ForegroundColor Yellow
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement