Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Requires Administrator privileges to run.
- #
- # This script automates the Windows 11 in-place upgrade using the
- # Windows11InstallationAssistant.exe with silent and unattended switches.
- #
- # It will:
- # 1. Define the URL for the Windows 11 Installation Assistant.
- # 2. Define the local path where the assistant executable will be stored.
- # 3. Check if the executable exists locally. If not, it will download it.
- # 4. Construct the command with the /QuietInstall, /SkipEULA, /Auto Upgrade, and /NoRestartUI switches.
- # 5. Execute the Installation Assistant.
- # 6. Check the exit code of the Installation Assistant.
- # 7. If a reboot is required (common for successful upgrades), it will trigger a reboot.
- # 8. Log important steps to a transcript file.
- # --- Configuration ---
- $installationAssistantUrl = "https://go.microsoft.com/fwlink/?linkid=2171764" # Official Microsoft direct download link for the assistant
- $downloadPath = "$env:TEMP\Windows11InstallationAssistant.exe" # Store in TEMP directory
- $logFilePath = "$env:SystemRoot\Logs\Windows11UpgradeAssistant.log" # Log file for this script's actions
- $upgradeLogDir = "$env:SystemRoot\Logs\Panther" # Standard Windows setup logs location
- # --- Start Logging ---
- # Create the log directory if it doesn't exist
- if (-not (Test-Path (Split-Path $logFilePath))) {
- New-Item -ItemType Directory -Path (Split-Path $logFilePath) -Force
- }
- Start-Transcript -Path $logFilePath -Append
- Write-Host "--- Starting Windows 11 In-Place Upgrade Script ---"
- Write-Host "Timestamp: $(Get-Date)"
- Write-Host "Script executed as: $([Security.Principal.WindowsIdentity]::GetCurrent().Name)"
- Write-Host "Downloading to: $downloadPath"
- # --- Step 1: Download Windows 11 Installation Assistant if not present ---
- if (-not (Test-Path $downloadPath)) {
- Write-Host "Windows 11 Installation Assistant not found locally. Downloading from Microsoft..."
- try {
- Invoke-WebRequest -Uri $installationAssistantUrl -OutFile $downloadPath -UseBasicParsing
- Write-Host "Download complete: $downloadPath"
- }
- catch {
- Write-Error "Failed to download Windows 11 Installation Assistant: $($_.Exception.Message)"
- Write-Host "Upgrade script aborted."
- Stop-Transcript
- exit 1 # Exit with error code
- }
- }
- else {
- Write-Host "Windows 11 Installation Assistant found locally: $downloadPath. Skipping download."
- }
- # --- Step 2: Clear previous setup logs (optional but recommended for fresh diagnosis) ---
- Write-Host "Cleaning up previous setup log directories for a fresh start..."
- try {
- if (Test-Path "$env:SystemRoot\$WINDOWS.~BT") { Remove-Item "$env:SystemRoot\$WINDOWS.~BT" -Recurse -Force -ErrorAction SilentlyContinue }
- if (Test-Path "$env:SystemRoot\$WINDOWS.~WS") { Remove-Item "$env:SystemRoot\$WINDOWS.~WS" -Recurse -Force -ErrorAction SilentlyContinue }
- # Also clear logs specific to old attempts
- if (Test-Path "$env:SystemRoot\Logs\UpgradeLog") { Remove-Item "$env:SystemRoot\Logs\UpgradeLog" -Recurse -Force -ErrorAction SilentlyContinue }
- if (Test-Path "$env:SystemRoot\System32\SafeReboot") { Remove-Item "$env:SystemRoot\System32\SafeReboot" -Recurse -Force -ErrorAction SilentlyContinue }
- Write-Host "Previous setup directories and logs cleaned."
- }
- catch {
- Write-Warning "Could not fully clean previous setup directories. Might be in use. Error: $($_.Exception.Message)"
- }
- # --- Step 3: Execute Windows 11 Installation Assistant ---
- Write-Host "Executing Windows 11 Installation Assistant..."
- $arguments = "/QuietInstall /SkipEULA /Auto Upgrade /NoRestartUI"
- Write-Host "Command: ""$downloadPath"" $arguments"
- # Use Start-Process for proper handling of GUI applications and their exit codes
- $process = Start-Process -FilePath $downloadPath -ArgumentList $arguments -Wait -PassThru -NoNewWindow
- $exitCode = $process.ExitCode
- Write-Host "Windows 11 Installation Assistant exited with code: $exitCode"
- # --- Step 4: Handle Exit Codes and Reboot ---
- # Common success codes for setup.exe / upgrade assistant:
- # 0: Success (no reboot needed, or handled by assistant itself)
- # 1: Generic failure (check logs)
- # 20: Reboot required for success
- # 259: Pending reboot (the installer is waiting for a reboot to continue)
- # 3010: SUCCESS_REBOOT_REQUIRED (often used by installers, equivalent to 20/259 for setup)
- if ($exitCode -eq 0 -or $exitCode -eq 20 -or $exitCode -eq 259 -or $exitCode -eq 3010) {
- Write-Host "Installation Assistant reported success or a pending reboot."
- Write-Host "Review Windows setup logs in '$upgradeLogDir' for details on upgrade status."
- # Trigger a reboot if the assistant indicated it's needed or if it's a common success/pending state.
- # We add a small delay to ensure logs are flushed.
- Write-Host "Initiating system reboot in 10 seconds..."
- Start-Sleep -Seconds 10
- Restart-Computer -Force
- }
- else {
- Write-Error "Windows 11 Installation Assistant reported an unexpected error ($exitCode)."
- Write-Error "Please check '$upgradeLogDir\setupact.log' and '$upgradeLogDir\setuperr.log' for details."
- Write-Host "Upgrade script finished with error."
- }
- # --- End Logging ---
- Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment