Determining 32/64 bit in Powershell

前端 未结 6 1924
梦谈多话
梦谈多话 2021-01-02 04:14

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

相关标签:
6条回答
  • 2021-01-02 04:57
    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.
    
    0 讨论(0)
  • 2021-01-02 04:58

    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"
    }
    
    0 讨论(0)
  • 2021-01-02 05:01
    [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).

    0 讨论(0)
  • 2021-01-02 05:06

    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"
    }
    
    0 讨论(0)
  • 2021-01-02 05:11

    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"
    }
    
    0 讨论(0)
  • 2021-01-02 05:13

    Two lines smashed togther for a nice one-liner:

    Write-Host "64bit process?:"$([Environment]::Is64BitProcess) ;Write-Host "64bit OS?:"$([Environment]::Is64BitOperatingSystem);
    
    0 讨论(0)
提交回复
热议问题