如何在PowerShell中检查要安装的可执行文件是32位还是64位?

问题描述:

如何检查将在PowerShell中安装的可执行文件是32位还是64位?如何在PowerShell中检查要安装的可执行文件是32位还是64位?

在PowerShell中是否有任何预定义的功能来做到这一点?

PowerShell有很多先进的PE header解析器。

这里有一个简单的函数,只需读取Machine Type领域:

function Is64bit([string]$path) { 
    try { 
     $stream = [IO.File]::OpenRead($path) 
    } catch { 
     throw "Cannot open file $path" 
    } 
    $reader = [IO.BinaryReader]$stream 

    if ($reader.ReadChars(2) -join '' -ne 'MZ') { throw 'Not an executable' } 

    $stream.position = 0x3C 
    $stream.position = $reader.ReadUInt32() # go to COFF 
    if ($reader.ReadUInt32() -ne 0x00004550) { throw 'Not a PE executable' } 

    return $reader.ReadUInt16() -eq 0x8664 # machine type 
} 

用法:

Is64bit C:\Windows\explorer.exe