Thanks to BACON's link to a closely related question with this answer, the following concise solution is possible, which works from both 32-bit and 64-bit PowerShell sessions:
$pointerSizeInBytes = (4, 8)[[Environment]::Is64BitOperatingSystem]
A [bool]
value interpreted as an array index ([int]
) maps to either 0
($false
) or 1
($true
), which is used here to select the appropriate value from array 4, 8
.
Here's the original form of the answer, which may have some related information of interest:
A simple test, assuming that you're always running from a 32-bit PowerShell instance:
$is64Bit = Test-Path C:\Windows\SysNative
32-bit processes (only) on 64-bit systems see the 64-bit SYSTEM32 (sic) directory as C:\Windows\SysNative
However, the following works from both 32-bit and 64-bit sessions:
$is64Bit = Test-Path 'Env:ProgramFiles(x86)'
Only on 64-bit systems does an automatically defined ProgramFiles(x86)
environment variable exist alongside the ProgramFiles
variable.
To get the OS-native pointer size in bytes:
$pointerSizeInBytes = (4, 8)[[bool] ${env:ProgramFiles(x86)}]
${env:ProgramFiles(x86)}
uses namespace variable notation to return the value of env. var. ProgramFiles(x86)
directly; casting a string value to [bool]
returns $true
only for non-empty strings; a [bool]
interpreted as an array index ([int]
) maps to either 0
($false
) or 1
($true
), which is used here to select the appropriate value from array 4, 8
.