ResettoOOBE.ps1
|
<#PSScriptInfo
.VERSION 1.0.0 .GUID 3ce4dbc6-ca15-4343-a086-498479247738 .AUTHOR Dylan Descamps .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION Prepare Windows for Autopilot OOBE after SCCM Task Sequence #> Param() $ScriptBlock = @' $LogPath = "C:\Windows\Temp\PrepareOOBE.log" function Write-Log { param( [string]$Message, [string]$Level = "Info" ) $Stamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Add-Content ` -Path $LogPath ` -Value "[$Stamp] [$Level] $Message" ` -ErrorAction SilentlyContinue } Write-Log "PrepareOOBE started" # Leave Azure AD registration Write-Log "Running dsregcmd /leave" Start-Process ` -FilePath "dsregcmd.exe" ` -ArgumentList "/leave" ` -Wait ` -NoNewWindow ` -ErrorAction SilentlyContinue # Uninstall SCCM if (Test-Path "C:\Windows\ccmsetup\ccmsetup.exe") { Write-Log "Starting ccmsetup /Uninstall" Start-Process ` -FilePath "C:\Windows\ccmsetup\ccmsetup.exe" ` -ArgumentList "/Uninstall" ` -Wait ` -NoNewWindow ` -ErrorAction SilentlyContinue Write-Log "ccmsetup /Uninstall completed" Start-Sleep -Seconds 60 } else { Write-Log "ccmsetup.exe not found" -Level Warning } # Stop SCCM services @( "CcmExec", "ccmsetup" ) | ForEach-Object { if (Get-Service -Name $_ -ErrorAction SilentlyContinue) { Write-Log "Stopping service $_" Stop-Service ` -Name $_ ` -Force ` -ErrorAction SilentlyContinue } } # Remove SCCM registry Write-Log "Removing SCCM registry entries" @( "HKLM:\SOFTWARE\Microsoft\CCM", "HKLM:\SOFTWARE\Microsoft\CCMSetup", "HKLM:\SOFTWARE\Microsoft\SMS" ) | ForEach-Object { if (Test-Path $_) { Remove-Item ` -Path $_ ` -Recurse ` -Force ` -ErrorAction SilentlyContinue if (-not (Test-Path $_)) { Write-Log "Verified removed: $_" } else { Write-Log "WARNING: Failed to remove: $_" -Level Warning } } else { Write-Log "Not found (OK): $_" } } # Remove SCCM directories Write-Log "Removing SCCM directories" @( "C:\Windows\CCM", "C:\Windows\CCMCache", "C:\Windows\CCMSetup", "C:\Windows\SMSCFG.ini", "C:\MININT" ) | ForEach-Object { if (Test-Path $_) { Remove-Item ` -Path $_ ` -Recurse ` -Force ` -ErrorAction SilentlyContinue if (-not (Test-Path $_)) { Write-Log "Verified removed: $_" } else { Write-Log "WARNING: Failed to remove: $_" -Level Warning } } else { Write-Log "Not found (OK): $_" } } # Remove SMS .mif files Write-Log "Removing SMS .mif files" $MifFiles = Get-ChildItem ` -Path "C:\Windows" ` -Filter "sms*.mif" ` -ErrorAction SilentlyContinue if ($MifFiles.Count -gt 0) { $MifFiles | Remove-Item ` -Force ` -ErrorAction SilentlyContinue Write-Log "Removed $($MifFiles.Count) SMS .mif files" } else { Write-Log "No SMS .mif files found" } # Remove TS post action Write-Log "Removing SMSTSPostAction" Remove-ItemProperty ` -Path "HKLM:\SOFTWARE\Microsoft\SMS\Task Sequence" ` -Name "SMSTSPostAction" ` -ErrorAction SilentlyContinue # Remove cached unattend files Write-Log "Removing cached unattend files" @( "C:\Windows\Panther\Unattend.xml", "C:\Windows\Panther\unattend" ) | ForEach-Object { if (Test-Path $_) { Remove-Item ` -Path $_ ` -Recurse ` -Force ` -ErrorAction SilentlyContinue Write-Log "Removed $_" } } # Remove setup state remnants Write-Log "Removing setup state remnants" Remove-ItemProperty ` -Path "HKLM:\SYSTEM\Setup" ` -Name "CmdLine" ` -ErrorAction SilentlyContinue Remove-ItemProperty ` -Path "HKLM:\SYSTEM\Setup" ` -Name "SetupType" ` -ErrorAction SilentlyContinue # Remove DisableCMDRequest.TAG Write-Log "Removing DisableCMDRequest.TAG" $TagPath = "C:\Windows\Setup\Scripts\DisableCMDRequest.TAG" if (Test-Path $TagPath) { Remove-Item ` -Path $TagPath ` -Force ` -ErrorAction SilentlyContinue Write-Log "Verified removed: DisableCMDRequest.TAG" } else { Write-Log "DisableCMDRequest.TAG not found" } # Wait for Autopilot backend sync Write-Log "Waiting 15 minutes for Autopilot profile propagation" Start-Sleep -Seconds 900 # Launch OOBE Write-Log "Launching sysprep /oobe /reboot" Start-Process ` -FilePath "C:\Windows\System32\Sysprep\Sysprep.exe" ` -ArgumentList "/oobe /reboot" ` -NoNewWindow ` -Wait Write-Log "PrepareOOBE finished" '@ New-Item ` -ItemType Directory ` -Path "C:\Windows\Temp" ` -Force ` -ErrorAction SilentlyContinue | Out-Null New-Item ` -ItemType File ` -Path "C:\Windows\Temp\PrepareOOBE.ps1" ` -Value $ScriptBlock ` -Force | Out-Null Write-Output "PrepareOOBE.ps1 created at C:\Windows\Temp\PrepareOOBE.ps1" |