Bendo1984
Bendo1984

Reputation: 27

powershell determine os architecture

I have a problem with this code. It prints "x86 operating system", even though the write-host, $OSArchitecture, states the architecture is 64-bit.

$OSArchitecture = (Get-WmiObject -Class Win32_OperatingSystem | Select-Object    OSArchitecture -ErrorAction Stop).OSArchitecture
write-host = $OSArchitecture

if ($OSArchitecture -eq '*64*') 
{
    Write-Host "x64 operating system" 
    $Version = Get-ChildItem hklm:\software\wow6432node\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion
} 
else 
{
    Write-Host "x86 operating system"
    $Version = Get-ChildItem hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion 
}

Update: I get this error: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".

Upvotes: 0

Views: 5347

Answers (3)

Novakov
Novakov

Reputation: 3095

On x64 system [Environment]::Is64BitOperatingSystem gives $true. Therefore, you could do this:

if ([Environment]::Is64BitOperatingSystem) 
{
    Write-Host "x64 operating system" 
    $Version = Get-ChildItem hklm:\software\wow6432node\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion
} 
else 
{
    Write-Host "x86 operating system"
    $Version = Get-ChildItem hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object {Get-ItemProperty $_.pspath} | Where-Object {
    $_.DisplayName -Eq 'Microsoft Lync 2013'} | Select-Object DisplayVersion 
}

Upvotes: 5

Corrected code.

$OSArchitecture=Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty OSArchitecture
if ($OSArchitecture -eq "64-bit") {
  Write-Output "x64 operating system" 
  $Path="HKLM:\software\wow6432node\microsoft\windows\currentversion\uninstall"
} 
else {
  Write-Output "x86 operating system"
  $Path="HKLM:\software\microsoft\windows\currentversion\uninstall"
}

$Version=Get-ChildItem -Path "HKLM:\software\wow6432node\microsoft\windows\currentversion\uninstall" | ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object { $_.DisplayName -eq 'Microsoft Lync 2013'} | Select-Object -ExpandProperty DisplayVersion

Upvotes: 1

CB.
CB.

Reputation: 60976

You can use this for this job

if ([System.IntPtr]::Size -eq 4) { "32-bit" } else { "64-bit" }

or change in your code

if ($OSArchitecture -match '64') 

Upvotes: -2

Related Questions