ChangeUserNameAndPath

#Change user profile name and path

#input for the user
$oldname = Read-Host -Prompt 'Input then name of user you want to change:'
Write-Host 'You write' $oldname

#input for name name

$newname = Read-Host -Prompt 'Input then name of user you want to change:'
Write-Host 'You write as new name' $newname
$currentuser=$env:username

Write-Host $currentuser

#to avoid changing current user
if ($currentuser=$oldname) {
 Write-Output 'You can''t change the user you are logged in'
 exit
}

#change registry path
$sid=Get-LocalUser -Name $oldname  | Select-Object -ExpandProperty SID 
$path='HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\'+$sid
$currentpath=Get-ItemProperty  -path $path  | select -ExpandProperty ProfileImagePath
write-host 'Current path:' $currentpath
#building the new path
$newpath =$currentpath.Substring(0, $currentpath.lastIndexOf('\'))
$newpath+="\"+$newname

Set-Itemproperty -path $path -Name 'ProfileImagePath' -value $newpath
$changedpath=Get-ItemProperty  -path $path  | select -ExpandProperty ProfileImagePath
write-host 'Changed path:' $changedpath

#change foldername
Rename-Item -Path $currentpath -NewName $newpath

#change username

Rename-LocalUser -Name $oldname -NewName $newname