PowerShell: Detect Windows Version

This little snippet is reusable on many occasions where Windows version targeting is required.

function detectWindowsVersion{
# Display Windows Version Name
# $versionDigits=[Environment]::OSVersion.Version
# (Get-WmiObject -class Win32_OperatingSystem).Caption
# (Get-WmiObject -class Win32_OperatingSystem).Caption
$windowsOS=Get-WmiObject -class Win32_OperatingSystem
$windowsName=$windowsOS.Caption
$windowsServicePack=$windowsOS.ServicePackMajorVersion
$releaseID=(Get-ItemProperty "REGISTRY::HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseID;

switch -wildcard ($windowsName){
"Microsoft Windows XP*"{
"Windows XP is way out of date. Turn it off asap."
}
"Microsoft Windows 7*"{
"$windowsName has been detected...";
if ($windowsServicePack -eq 1){
# Windows 7 Service Pack 1 / Windows Server 2008 R2 Service Pack 1
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/05/windows6.1-kb4103718-x64_c051268978faef39e21863a95ea2452ecbc0936d.msu"
$GLOBAL:kb="KB4103718"
}else{
"Windows 7 is no longer being supported by Michael Shop..."
}
break;
}
"Microsoft Windows Server 2008*"{
"$windowsName has been detected...";
if ($windowsName -like "Microsoft Windows Server 2008 R2*"){
# Windows 7 Service Pack 1 / Windows Server 2008 R2 Service Pack 1
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/05/windows6.1-kb4103718-x64_c051268978faef39e21863a95ea2452ecbc0936d.msu"
$GLOBAL:kb="KB4103718"
}else{
"Windows 2008 is no longer being supported by Michael Shop..."
}
break;
}
"Microsoft Windows Server 2012*"{
if ($windowsName -like "Microsoft Windows Server 2012 R2*"){
"Microsoft Windows Server 2012 R2 has been detected...";
# Windows 8.1 / Windows Server 2012 R2
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/05/windows8.1-kb4103725-x64_cdf9b5a3be2fd4fc69bc23a617402e69004737d9.msu"
$GLOBAL:kb="KB4103725"
}else{
"Microsoft Windows Server 2012 has been detected...";
# Windows Server 2012 Default
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/04/windows8-rt-kb4103730-x64_1f4ed396b8c411df9df1e6755da273525632e210.msu"
$GLOBAL:kb="KB4103730"
}
break;
}
"Microsoft Windows Server 2016*"{
"$windowsName release ID $releaseID has been detected...";
switch ($releaseID){
1607{
# RS1 - Windows 10 version 1607 / Windows Server 2016
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/05/windows10.0-kb4103723-x64_2adf2ea2d09b3052d241c40ba55e89741121e07e.msu"
$GLOBAL:kb="kb4103723"
}
1709{
# RS3 - Windows 10 version 1709 / Windows Server 2016 version 1709
$GLOBAL:source = "http://download.windowsupdate.com/c/msdownload/update/software/secu/2018/05/windows10.0-kb4103727-x64_c217e7d5e2efdf9ff8446871e509e96fdbb8cb99.msu"
$GLOBAL:kb="KB4103727"
}
1803{
# RS4 - Windows 10 1803 / Windows Server 2016 version 1803
$GLOBAL:source = "http://download.windowsupdate.com/c/msdownload/update/software/secu/2018/05/windows10.0-kb4103721-x64_fcc746cd817e212ad32a5606b3db5a3333e030f8.msu"
$GLOBAL:kb="KB4103721"
}
}
break;
}
"Microsoft Windows 10*"{
"$windowsName release ID $releaseID has been detected...";
switch ($releaseID){
1607{
# RS1 - Windows 10 version 1607
$GLOBAL:source = "http://download.windowsupdate.com/d/msdownload/update/software/secu/2018/05/windows10.0-kb4103723-x64_2adf2ea2d09b3052d241c40ba55e89741121e07e.msu"
$GLOBAL:kb="KB4103723"
}
1703{
# RS2 - Windows 10 version 1703
$GLOBAL:source = "http://download.windowsupdate.com/c/msdownload/update/software/secu/2018/05/windows10.0-kb4103731-x64_209b6a1aa4080f1da0773d8515ff63b8eca55159.msu"
$GLOBAL:kb="KB4103731"
}
1709{
# RS3 - Windows 10 version 1709 / Windows Server 2016 version 1709
$GLOBAL:source = "http://download.windowsupdate.com/c/msdownload/update/software/secu/2018/05/windows10.0-kb4103727-x64_c217e7d5e2efdf9ff8446871e509e96fdbb8cb99.msu"
$GLOBAL:kb="KB4103727"
}
1803{
# RS4 - Windows 10 1803 / Windows Server 2016 version 1803
$GLOBAL:source = "http://download.windowsupdate.com/c/msdownload/update/software/secu/2018/05/windows10.0-kb4103721-x64_fcc746cd817e212ad32a5606b3db5a3333e030f8.msu"
$GLOBAL:kb="KB4103721"
}
}
break;
}
}
}

detectWindowsVersion;

Leave a Reply

Your email address will not be published. Required fields are marked *