{"id":4169,"date":"2022-08-25T18:55:53","date_gmt":"2022-08-26T02:55:53","guid":{"rendered":"https:\/\/tungle.ca\/?p=4169"},"modified":"2023-03-13T17:34:03","modified_gmt":"2023-03-14T01:34:03","slug":"query-windows-current-os-build-via-powershell","status":"publish","type":"post","link":"https:\/\/tungle.ca\/?p=4169","title":{"rendered":"Query Windows current OS build via PowerShell"},"content":{"rendered":"\n<p>Below is a PS code that is used to query current OS build via PowerShell.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell line-numbers\">function Get-WindowsVersion{\n[CmdletBinding()]\n\nParam\n(\n    [Parameter(Mandatory=$false,\n                ValueFromPipelineByPropertyName=$true,\n                ValueFromPipeline=$true\n                )]\n    [string[]]$ComputerName = $env:COMPUTERNAME\n)\n\nBEGIN {}\nPROCESS {\n\nForeach ($Computer in $ComputerName)\n{ \n    try {\n        #$ping = Test-Connection -computerName $Computer -count 1 -Quiet\n        #$ping = Test-NetConnection -computerName $Computer -port 5985 -InformationAction SilentlyContinue\n        #if($ping)\n            #{\n            #$code = Get-ItemProperty -PATH 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'\n            $Result = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ItemProperty -PATH 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'} -ErrorAction Stop \n            $properties = [ordered]@{\n                    ComputerName = $Computer\n                    'Windows Edition' = $Result.ProductName\n                    'Version' = $Result.ReleaseId\n                    'OS Build' = $Result.CurrentBuild + '.' + $Result.UBR}\n        }  catch {\n            #Write-Verbose \"Coundn't connect to $computer\"\n            $properties = [ordered]@{\n                    ComputerName = $Computer\n                    'Windows Edition' = $null\n                    'Version' = $null\n                    'OS Build' = $null\n                        } \n        } \n        finally {\n            $obj = New-Object -Typename PSCustomObject -Property $properties\n            Write-Output $obj \n        }\n}  \n}\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"477\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1-1024x477.png\" alt=\"\" class=\"wp-image-4172\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1-1024x477.png 1024w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1-300x140.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1-768x358.png 768w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1-200x93.png 200w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/22-1.png 1512w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-WindowsVersion -computer W2016-S1\nGet-WindowsVersion -ComputerName @('W2016-S1','DC3','DC1','DC2','Win10-PC') | sort Version -Descending<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"252\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/23-1.png\" alt=\"\" class=\"wp-image-4173\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/23-1.png 952w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/23-1-300x79.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/23-1-768x203.png 768w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/09\/23-1-200x53.png 200w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/figure>\n\n\n\n<p>(DC2 and DC3 are not running or we cannot access to the servers for some reasons . So, we will create a hash table and assign null value for these computers.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a PS code that is used to query current OS build via PowerShell. (DC2 and DC3 are not running or we cannot access to the servers for some reasons . So, we will create a hash table and assign null value for these computers.)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,7],"tags":[10,12],"class_list":["post-4169","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-powershell","tag-microsoft","tag-powershell"],"_links":{"self":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4169"}],"version-history":[{"count":3,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4169\/revisions"}],"predecessor-version":[{"id":4305,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4169\/revisions\/4305"}],"wp:attachment":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}