CMD Windows Updates (SILENT) - Neilitlib/MSP-Simple-Commands GitHub Wiki

  • Useful for machine prep

PS

  1. Set-ExecutionPolicy Bypass -Force -Confirm:$false; Install-PackageProvider -Name NuGet -Force -Confirm:$false; Install-Module PSWindowsUpdate -Force -Confirm:$false

  2. Import-Module PSWindowsUpdate; $dir = 'C:\temp\Win11'; try { if (-Not (Test-Path -Path $dir)) { New-Item -ItemType Directory -Path $dir -ErrorAction Stop } } catch { Write-Error "Failed to create directory: $_"; exit 1 }; $webClient = New-Object System.Net.WebClient; $url = 'https://go.microsoft.com/fwlink/?linkid=2171764'; $file = "$($dir)\Windows11InstallationAssistant.exe"; try { $webClient.DownloadFile($url, $file) } catch { Write-Error "Failed to download file: $_"; exit 1 }; try { Start-Process -FilePath $file -ArgumentList "/quietinstall /skipeula /autoupgrade /NoRestartUI /copylogs $dir" -ErrorAction Stop } catch { Write-Error "Failed to start process: $_"; exit 1 }


This PowerShell script performs the following actions: Sure, let's break down the script step-by-step, putting each section of code between single quotes:

  1. 'Import-Module PSWindowsUpdate':

    • This command imports the PSWindowsUpdate module, which is necessary for managing Windows updates via PowerShell.
  2. '$dir = 'C:\temp\Win11'':

    • This line sets the variable $dir to the path C:\temp\Win11, which will be used to store the downloaded file.
  3. 'try { if (-Not (Test-Path -Path $dir)) { New-Item -ItemType Directory -Path $dir -ErrorAction Stop } } catch { Write-Error "Failed to create directory: $_"; exit 1 }':

    • try: Starts a block of code that will attempt to execute.
    • if (-Not (Test-Path -Path $dir)): Checks if the directory specified by $dir does not exist.
    • New-Item -ItemType Directory -Path $dir -ErrorAction Stop: Creates the directory if it does not exist, and stops execution if an error occurs.
    • catch { Write-Error "Failed to create directory: $_"; exit 1 }: If an error occurs during directory creation, it writes an error message and exits the script with code 1.
  4. '$webClient = New-Object System.Net.WebClient':

    • This line creates a new instance of the System.Net.WebClient class, which will be used to download files from the web.
  5. '$url = 'https://go.microsoft.com/fwlink/?linkid=2171764'':

    • Sets the variable $url to the URL of the file to be downloaded.
  6. '$file = "$($dir)\Windows11InstallationAssistant.exe"':

    • Sets the variable $file to the path where the downloaded file will be saved, combining $dir and the filename.
  7. 'try { $webClient.DownloadFile($url, $file) } catch { Write-Error "Failed to download file: $_"; exit 1 }':

    • try: Starts a block of code that will attempt to execute.
    • $webClient.DownloadFile($url, $file): Downloads the file from the URL specified by $url and saves it to the path specified by $file.
    • catch { Write-Error "Failed to download file: $_"; exit 1 }: If an error occurs during the download, it writes an error message and exits the script with code 1.
  8. 'try { Start-Process -FilePath $file -ArgumentList "/quietinstall /skipeula /autoupgrade /NoRestartUI /copylogs $dir" -ErrorAction Stop } catch { Write-Error "Failed to start process: $_"; exit 1 }':

    • try: Starts a block of code that will attempt to execute.
    • Start-Process -FilePath $file -ArgumentList "/quietinstall /skipeula /autoupgrade /NoRestartUI /copylogs $dir" -ErrorAction Stop: Starts the process to run the downloaded file with the specified arguments, and stops execution if an error occurs.
    • catch { Write-Error "Failed to start process: $_"; exit 1 }: If an error occurs during the process start, it writes an error message and exits the script with code 1.

This script ensures that each step is executed correctly and handles errors gracefully, providing clear error messages and exiting if something goes wrong.

In summary, this script bypasses the PowerShell execution policy to allow scripts to run, installs the NuGet package provider and the PSWindowsUpdate module without asking for user confirmation, imports the PSWindowsUpdate module, and then fetches and installs all updates in the specified categories from Microsoft Update. It accepts all updates without prompting the user and automatically reboots the system if needed. Please note that running scripts like this should be done with caution, as they can make significant changes to your system. Always ensure you have a backup of your important data before running such scripts. Also, running such scripts with administrative privileges could have system-wide effects.