Detect Windows 10 Version and update to latest build via powershell

asked 2021-04-18T08:36:37.523+00:00
Gilbert Reinhardt 1 Reputation point

Hi

Im looking for a script if someone has made it that will check the windows build.
If it's less than 19042.572 then do the following:

Something Like

If

(Get-CimInstance Win32_OperatingSystem).BuildNumber < 19042 then

Set-ExecutionPolicy unrestricted

Install-Module PSWindowsUpdate

Import-Module PSWindowsUpdate

Get-WindowsUpdate -KBArticleID KBXXXXXX (This being the Latest Windows 20H2)

Install-WindowsUpdate -AcceptAll -AutoReboot

Windows 10 Setup
Windows 10 Setup
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Setup: The procedures involved in preparing a software program or application to operate within a computer or mobile device.
1,728 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-04-19T03:36:37.493+00:00
    Ian Xue (Shanghai Wicresoft Co., Ltd.) 18,351 Reputation points Microsoft Employee

    Hi,

    The comparison operator in PowerShell is "-lt", not "<" and you should specify the if block using the curly braces.

    if((Get-CimInstance Win32_OperatingSystem).BuildNumber -le 19042)  
    {  
        Set-ExecutionPolicy unrestricted  
        Install-Module PSWindowsUpdate  
        Import-Module PSWindowsUpdate  
        Get-WindowsUpdate -KBArticleID KBXXXXXX -Install -AcceptAll -AutoReboot  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments