Determining 32/64 bit in Powershell

爷,独闯天下 提交于 2019-12-05 00:11:40

问题


I am trying to create a couple lines of code that will pull from WMI if a machine is either 32/64 bit and then if it is 64 do this .... if it is 32bit do this...

Can anyone help?


回答1:


There's two boolean static methods in the Environment you can inspect and compare, one looks at the PowerShell process, one looks at the underlying OS.

if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
{
"PowerShell process does not match the OS"
}



回答2:


Random discussion about it

Assuming you are running at least Windows 7, the following should work.

Including a sample that worked for me in a 32 bit version of powershell running on a 64 bit machine:

gwmi win32_operatingsystem | select osarchitecture

Returns "64-bit" for 64 bit.

if ((gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit")
{
    #64 bit logic here
    Write "64-bit OS"
}
else
{
    #32 bit logic here
    Write "32-bit OS"
}



回答3:


[IntPtr]::Size -eq 4 # 32 bit

The size of an IntPtr will be 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine (https://msdn.microsoft.com/en-us/library/system.intptr.size.aspx).




回答4:


This is similar to a previous answer but will get a correct result regardless of 64-bit/64_bit/64bit/64bits format.

if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
{
#64bit code here
Write "64-bit OS"
}
else
{
#32bit code here
Write "32-bit OS"
}



回答5:


if($env:PROCESSOR_ARCHITECTURE -eq "x86"){"32-Bit CPU"}Else{"64-Bit CPU"}

-edit, sorry forgot to include more code to explain the usage.

if($env:PROCESSOR_ARCHITECTURE -eq "x86")
 {
#If the powershell console is x86, create alias to run x64 powershell console.
 set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"

$script2=[ScriptBlock]::Create("#your commands here, bonus is the script block expands variables defined above")

ps64 -command $script2
 }
 Else{
 #Otherwise, run the x64 commands.


来源:https://stackoverflow.com/questions/31977657/determining-32-64-bit-in-powershell

标签

工具导航Map

JSON相关