Upgrade Windows 11 at Home

# Ensure the script is run as an administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "Please run this script as an Administrator."
    exit 1
}

# Function to check available disk space
function Check-DiskSpace {
    $drive = Get-PSDrive -Name C

    # Required space for Windows 11 (64 GB)
    $requiredSpace = 64 * 1GB
    if ($drive.Free -lt $requiredSpace) {
        Write-Error "Not enough disk space available. You need at least 64 GB free."
        exit 1
    } else {
        Write-Host "Sufficient disk space available: $([math]::round($drive.Free / 1GB, 2)) GB free."
    }
}

# Function to trigger Windows Update
function Trigger-WindowsUpdate {
    try {
        Write-Host "Checking for updates..."
        $session = New-Object -ComObject Microsoft.Update.Session
        $searcher = $session.CreateUpdateSearcher()
        $search = $searcher.Search("IsInstalled=0")

        if ($search.Updates.Count -eq 0) {
            Write-Host "No pending updates found."
            return
        }

        Write-Host "$($search.Updates.Count) updates found."
        
        # Create a collection for updates to download
        $downloads = New-Object -ComObject Microsoft.Update.UpdateColl
        foreach ($update in $search.Updates) {
            Write-Host "Preparing to download: $($update.Title)"
            $downloads.Add($update)
        }
        
        # Download updates
        Write-Host "Downloading updates..."
        $downloader = $session.CreateUpdateDownloader()
        $downloader.Updates = $downloads
        $downloader.Download()
        
        Write-Host "Installing updates..."
        # Install updates
        $installer = $session.CreateUpdateInstaller()
        $installer.Updates = $downloads
        $installationResult = $installer.Install()

        Write-Host "Updates installed successfully."
        
    } catch {
        Write-Error "Failed to trigger Windows Update: $_"
    }
}

# Enable Registry Keys for Windows 11 Upgrade
function Set-RegistryKeys {
    try {
        # Using PowerShell's registry cmdlets to set the keys
        Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate" -Name "TargetReleaseVersion" -Value 1 -Type DWord -Force
        Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate" -Name "TargetReleaseVersionInfo" -Value "21H2" -Type String -Force
        Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate" -Name "ProductVersion" -Value "Windows 11" -Type String -Force

        Write-Host "Registry keys set successfully."
    } catch {
        Write-Error "Failed to set registry keys: $_"
        exit 1
    }
}

# Main Execution
Check-DiskSpace
Set-RegistryKeys
Trigger-WindowsUpdate

# Check and install upgrades
try {
    Write-Host "Checking for Windows 11 upgrade..."
    $upgradeSearch = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher().Search("IsInstalled=0")

    if ($upgradeSearch.Updates.Count -gt 0) {
        Write-Host "$($upgradeSearch.Updates.Count) potential upgrades found."

        # Install upgrades without trying to download
        $installer = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateInstaller()
        $installer.Updates = $upgradeSearch.Updates
        $installationResult = $installer.Install()

        Write-Host "Upgrade installation completed."
    } else {
        Write-Host "No upgrades available."
    }
} catch {
    Write-Error "Failed to trigger upgrade detection: $_"
}

Posted

in

, ,

by