Invoke-Reprofile
Invoke-Reprofile
Fix Corrupted User's Profiles with PowerShell
By Michael J. Thomas
Troubleshooting a user's profile is sometimes hard to pin point out where the problem is and that is why a lot of us techs will use the technique of re-profiling the user's profile and copying just their data back. To do this process of re-profiling a user manually, I will demonstrate this process with a user account named Ed. I would do the following steps to fix his corrupted profile:
- Logoff Ed
- Login with an Admin Account
- Rename C:\Users\Ed to C:\Users\Ed.old
- Edit the Registry HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileListNow\SID and find Ed's SID and rename it to the SID.old.
- Logoff Admin Account
- Login Ed to Create a New Profile for him.
- Copy Ed's Data from Ed.Old Folder to his New User Profile.
I automated the process by creating a PowerShell Script that can fix it remotely. The script renames the user's profile to user.old, backups the user's registry key to windows temp folder, and rename's the SID in the registry to SID.old . The only thing I did not include in this script is to copy the user's data over to their new profile when they log back in. Don't worry, I will include that in a future version.
I hope you enjoy this weeks PowerShell Weekly Script!
<# .Synopsis Invoke-Reprofile is used to reprofile a remote users profile. Author: Michael J. Thomas Created: 06/29/2019 Modified: 06/29/2019 Notes: WinRM must be Configured on Remote Computers and Remote Users must be Logged off. I have other functions for doing that. Not included with this example at this time. .DESCRIPTION Invoke-Reprofile renames the UserName to UserName.old in Users Folder, Backup User SID in Registry to Windows Temp Folder, and Renamed SID to SID.Old in ProfileList. If user profile is messed up on multiple systems, use this on multiple computers. .EXAMPLE Invoke-Reprofile -ComputerName "Computer01" -UserName "User01" .EXAMPLE Invoke-Reprofile -ComputerName "Computer01" -UserName "User01","User02" .EXAMPLE Invoke-Reprofile -ComputerName "Computer01","Computer02" -UserName "User01" .EXAMPLE Invoke-Reprofile -ComputerName "Computer01","Computer02" -UserName "User01","User02" #> function Invoke-ReProfile { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]] $ComputerName, [string[]] $UserName ) Begin { Write-Host "Invoking ReProfile Process on $ComputerName" } Process { Try{ Invoke-Command -ComputerName $ComputerName -ScriptBlock{ $SID = (New-Object System.Security.Principal.NTAccount($Using:UserName)).Translate([System.Security.Principal.SecurityIdentifier]).Value $TimeStamp = Get-Date -format yyyy-MM-dd-mm-ss-ff Rename-Item -path "$env:SystemDrive\Users\$Using:UserName" -newName "$Using:UserName.old" -Force -ErrorAction Stop Reg Export "Hkey_local_Machine\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$SID" $env:windir\temp\$Using:UserName$TimeStamp.reg Rename-Item -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$SID" -NewName "$($SID).old" -Force -ErrorAction Stop #Option for Removing The User Registry Key #Remove-Item -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$SID" -Force -Confirm:$false -Recurse } -ErrorAction Stop } Catch{ Write-Host $_.Exception.Message -ForegroundColor Red } } End { Write-Host "Completed Changing $UserName.old in Users Folder, Backup Registry to Windows Temp Folder, and Renamed Users SID in Registry ProfileList to SID.Old" -ForegroundColor Green Write-Host "Please have user login and copy their data from the $UserName.old Folder" -ForegroundColor Green } } | |
>_ Get-PowerShellWeekly Follow us by Email |
Thanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHello! I ran into your Automation of reprofiling user account. I'm not sure if you are still in this board as I see that this was posted some time back in 2019. I am new to scripting and powershell. How can I execute this scrip? Do I have to create a MODULE folder with your files in it?
DeleteYou need to make sure WinRM is enabled on the machine that you are going to run this function on. To enable WinRM from that computer run as admin the cmd prompt "winrm qc". Then from your computer run the Windows PowerShell ISE as Admin then copy the function above into it. You can then save the function as Invoke-Reprofile.ps1.
DeleteThen you run it and enter the command "Invoke-Reprofile -ComputerName "Computer01" -UserName "User01". Replace the Computer01 with your target computer and replace User01 with your target user that you want to reprofile. Before you reprofile I recommend rebooting the computer. Sorry the delay in response I did not get a notification of a comment and was looking back at some of these I've written. I do plan to return to writing more scripts. I've been really busy and had not had much free time.