Skip to content

Instantly share code, notes, and snippets.

@tksunw
Created December 1, 2021 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tksunw/7de80f19e558a885fb65cb950d230cc8 to your computer and use it in GitHub Desktop.
Save tksunw/7de80f19e558a885fb65cb950d230cc8 to your computer and use it in GitHub Desktop.
rename-homedir
function Rename-HomeDir {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[String]$OldName,
[Parameter(Mandatory=$true)]
[String]$NewName
)
"Getting user info"
$oldUser = Get-LocalUser $oldName
if(!($oldUser)) {
"Error: Old Username $olduser not found"
break
}
"Getting user SID"
$regpath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($oldUser.SID)"
"Getting user ProfileImagePath"
$olddir = (Get-ItemProperty -Path $regpath -Name ProfileImagePath).ProfileImagePath
$newdir = $olddir.replace($oldname, $newname)
"Setting new ProfileImagePath"
Set-ItemProperty -Path $regpath -Name ProfileImagePath -Value $newdir
"Renaming $olddir to $newdir"
Rename-Item -Path $olddir -NewName $newdir
"Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment