Guest User

Untitled

a guest
Jul 1st, 2025
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. # Requires Administrator privileges to run.
  2. #
  3. # This script automates the Windows 11 in-place upgrade using the
  4. # Windows11InstallationAssistant.exe with silent and unattended switches.
  5. #
  6. # It will:
  7. # 1. Define the URL for the Windows 11 Installation Assistant.
  8. # 2. Define the local path where the assistant executable will be stored.
  9. # 3. Check if the executable exists locally. If not, it will download it.
  10. # 4. Construct the command with the /QuietInstall, /SkipEULA, /Auto Upgrade, and /NoRestartUI switches.
  11. # 5. Execute the Installation Assistant.
  12. # 6. Check the exit code of the Installation Assistant.
  13. # 7. If a reboot is required (common for successful upgrades), it will trigger a reboot.
  14. # 8. Log important steps to a transcript file.
  15.  
  16. # --- Configuration ---
  17. $installationAssistantUrl = "https://go.microsoft.com/fwlink/?linkid=2171764" # Official Microsoft direct download link for the assistant
  18. $downloadPath = "$env:TEMP\Windows11InstallationAssistant.exe" # Store in TEMP directory
  19. $logFilePath = "$env:SystemRoot\Logs\Windows11UpgradeAssistant.log" # Log file for this script's actions
  20. $upgradeLogDir = "$env:SystemRoot\Logs\Panther" # Standard Windows setup logs location
  21.  
  22. # --- Start Logging ---
  23. # Create the log directory if it doesn't exist
  24. if (-not (Test-Path (Split-Path $logFilePath))) {
  25. New-Item -ItemType Directory -Path (Split-Path $logFilePath) -Force
  26. }
  27. Start-Transcript -Path $logFilePath -Append
  28.  
  29. Write-Host "--- Starting Windows 11 In-Place Upgrade Script ---"
  30. Write-Host "Timestamp: $(Get-Date)"
  31. Write-Host "Script executed as: $([Security.Principal.WindowsIdentity]::GetCurrent().Name)"
  32. Write-Host "Downloading to: $downloadPath"
  33.  
  34. # --- Step 1: Download Windows 11 Installation Assistant if not present ---
  35. if (-not (Test-Path $downloadPath)) {
  36. Write-Host "Windows 11 Installation Assistant not found locally. Downloading from Microsoft..."
  37. try {
  38. Invoke-WebRequest -Uri $installationAssistantUrl -OutFile $downloadPath -UseBasicParsing
  39. Write-Host "Download complete: $downloadPath"
  40. }
  41. catch {
  42. Write-Error "Failed to download Windows 11 Installation Assistant: $($_.Exception.Message)"
  43. Write-Host "Upgrade script aborted."
  44. Stop-Transcript
  45. exit 1 # Exit with error code
  46. }
  47. }
  48. else {
  49. Write-Host "Windows 11 Installation Assistant found locally: $downloadPath. Skipping download."
  50. }
  51.  
  52. # --- Step 2: Clear previous setup logs (optional but recommended for fresh diagnosis) ---
  53. Write-Host "Cleaning up previous setup log directories for a fresh start..."
  54. try {
  55. if (Test-Path "$env:SystemRoot\$WINDOWS.~BT") { Remove-Item "$env:SystemRoot\$WINDOWS.~BT" -Recurse -Force -ErrorAction SilentlyContinue }
  56. if (Test-Path "$env:SystemRoot\$WINDOWS.~WS") { Remove-Item "$env:SystemRoot\$WINDOWS.~WS" -Recurse -Force -ErrorAction SilentlyContinue }
  57. # Also clear logs specific to old attempts
  58. if (Test-Path "$env:SystemRoot\Logs\UpgradeLog") { Remove-Item "$env:SystemRoot\Logs\UpgradeLog" -Recurse -Force -ErrorAction SilentlyContinue }
  59. if (Test-Path "$env:SystemRoot\System32\SafeReboot") { Remove-Item "$env:SystemRoot\System32\SafeReboot" -Recurse -Force -ErrorAction SilentlyContinue }
  60. Write-Host "Previous setup directories and logs cleaned."
  61. }
  62. catch {
  63. Write-Warning "Could not fully clean previous setup directories. Might be in use. Error: $($_.Exception.Message)"
  64. }
  65.  
  66.  
  67. # --- Step 3: Execute Windows 11 Installation Assistant ---
  68. Write-Host "Executing Windows 11 Installation Assistant..."
  69. $arguments = "/QuietInstall /SkipEULA /Auto Upgrade /NoRestartUI"
  70. Write-Host "Command: ""$downloadPath"" $arguments"
  71.  
  72. # Use Start-Process for proper handling of GUI applications and their exit codes
  73. $process = Start-Process -FilePath $downloadPath -ArgumentList $arguments -Wait -PassThru -NoNewWindow
  74.  
  75. $exitCode = $process.ExitCode
  76. Write-Host "Windows 11 Installation Assistant exited with code: $exitCode"
  77.  
  78. # --- Step 4: Handle Exit Codes and Reboot ---
  79. # Common success codes for setup.exe / upgrade assistant:
  80. # 0: Success (no reboot needed, or handled by assistant itself)
  81. # 1: Generic failure (check logs)
  82. # 20: Reboot required for success
  83. # 259: Pending reboot (the installer is waiting for a reboot to continue)
  84. # 3010: SUCCESS_REBOOT_REQUIRED (often used by installers, equivalent to 20/259 for setup)
  85.  
  86. if ($exitCode -eq 0 -or $exitCode -eq 20 -or $exitCode -eq 259 -or $exitCode -eq 3010) {
  87. Write-Host "Installation Assistant reported success or a pending reboot."
  88. Write-Host "Review Windows setup logs in '$upgradeLogDir' for details on upgrade status."
  89.  
  90. # Trigger a reboot if the assistant indicated it's needed or if it's a common success/pending state.
  91. # We add a small delay to ensure logs are flushed.
  92. Write-Host "Initiating system reboot in 10 seconds..."
  93. Start-Sleep -Seconds 10
  94. Restart-Computer -Force
  95. }
  96. else {
  97. Write-Error "Windows 11 Installation Assistant reported an unexpected error ($exitCode)."
  98. Write-Error "Please check '$upgradeLogDir\setupact.log' and '$upgradeLogDir\setuperr.log' for details."
  99. Write-Host "Upgrade script finished with error."
  100. }
  101.  
  102. # --- End Logging ---
  103. Stop-Transcript
  104.  
Advertisement
Add Comment
Please, Sign In to add comment