Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Save this file as: Rename-WindowsUser.ps1
- # Run from another admin user account (e.g. tempadmin)
- param (
- [string]$OldUserName = "swaru",
- [string]$NewUserName = "Swarup"
- )
- Write-Host "Renaming Windows user folder..." -ForegroundColor Cyan
- # Define paths
- $oldPath = "C:\Users\$OldUserName"
- $newPath = "C:\Users\$NewUserName"
- # Check if old folder exists
- if (-Not (Test-Path $oldPath)) {
- Write-Host "❌ Old user folder not found: $oldPath" -ForegroundColor Red
- exit
- }
- # Rename folder
- try {
- Rename-Item -Path $oldPath -NewName $NewUserName
- Write-Host "✅ Folder renamed: $oldPath → $newPath" -ForegroundColor Green
- } catch {
- Write-Host "❌ Failed to rename folder. Make sure you're not logged into that user." -ForegroundColor Red
- exit
- }
- # Modify registry path
- Write-Host "Updating Registry path..." -ForegroundColor Cyan
- # Get list of all profiles in registry
- $regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
- $profiles = Get-ChildItem $regPath
- foreach ($profile in $profiles) {
- $profileImagePath = Get-ItemProperty -Path $profile.PSPath | Select-Object -ExpandProperty ProfileImagePath -ErrorAction SilentlyContinue
- if ($profileImagePath -eq $oldPath) {
- Set-ItemProperty -Path $profile.PSPath -Name ProfileImagePath -Value $newPath
- Write-Host "✅ Registry updated for SID: $($profile.PSChildName)" -ForegroundColor Green
- }
- }
- Write-Host "`n🎉 DONE! You can now log in with your original account and see the new path." -ForegroundColor Yellow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement