-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStart-ProfileRebuild.ps1
More file actions
66 lines (51 loc) · 3.04 KB
/
Start-ProfileRebuild.ps1
File metadata and controls
66 lines (51 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Start-ProfileRebuild { # Start function
<#
.SYNOPSIS
Starts rebuilding windows profile by removing registry keys associated with user account, renaming windows profile, and rebooting to release any locks on profile
.DESCRIPTION
Updates C:\Users\<Profile> folder in Users directory to OLD-ProfileName
Removes registry keys associated with SID: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\<UserSID>
Checks if machine has been online for more than an hour
Reboots to release locks on profile
.PARAMETER UserName
Username of the user account being rebuilt
.PARAMETER ProfileName
Profile folder name of the profile being rebuilt
.PARAMETER ComputerName
Hostname of the machine you're running the script against
.EXAMPLE
Start-ProfileRebuild -ComputerName "Shaneserver" -ProfileName "bob" -UserName "bob"
.NOTES
Must be ran before Move-ProfileData.ps1
#>
[CmdletBinding()]
Param (
[Parameter(ValueFromPipelineByPropertyName = $True, Mandatory = $True)]
[String]$ComputerName,
[Parameter(Mandatory = $True)]
[String]$ProfileName,
[Parameter(Mandatory = $True)]
[String]$UserName
)
begin { # Begin block
$LastBootUpTime = Invoke-Command -ComputerName $ComputerName -ScriptBlock { Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime }
$LocalDateTime = Invoke-Command -ComputerName $ComputerName -ScriptBlock { Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty LocalDateTime }
$TimeDifference = New-TimeSpan -Start $LocalDateTime -End $LastBootUpTime
if ($TimeDifference.Hours -ne 0) { # Checks if machine has been online for more than an hour
Write-Warning "Power cycling machine to release any profile locks as it's been online for more than one hour"
Write-Warning "Re-run script after reboot completes"
Start-Sleep -Seconds 10
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Restart-Computer -Force } # Reboot workstation to release locks on profile
exit # Terminate script and must be reran
}
}
process { # Process block
Set-Location "C:\Users"; Rename-Item $ProfileName "OLD-$ProfileName" # Renames user profile by appending the word "OLD"
$SID = Get-CimInstance -ComputerName $ComputerName -ClassName Win32_UserAccount | Where-Object { $_.Name -eq $UserName } | Select-Object -ExpandProperty SID # Extract SID from the user account
Remove-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$SID" -recurse # Remove the registry keys associated with user profile
Write-Warning "Profile renamed, registry keys cleared, rebooting machine to finalize changes"
Write-Warning "Please sign back into windows with user credentials after reboot"
Start-Sleep -Seconds 10
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Restart-Computer -Force } # Reboot workstation to release locks on profile
}
} # End function