Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

x32 or x64 .EXE files

Aug
2,217
101
In Reference to https://jpsoft.com/forums/threads/indicate-exe-and-dll-type-in-dir-command.12175/

I have been working with Microsoft Bing Co-Pilot,
and am starting to create a solution using PowerShell.
Code:
function Get-PEHdr {
    param (
        [string]$Path
    )

    $stream = [System.IO.File]::OpenRead($Path)
    $reader = New-Object System.IO.BinaryReader($stream)

    $stream.Seek(0x3C, [System.IO.SeekOrigin]::Begin) | Out-Null
    $peHeaderOffset = $reader.ReadInt32()

    $stream.Seek($peHeaderOffset, [System.IO.SeekOrigin]::Begin) | Out-Null
    $peHeader = $reader.ReadBytes(24)

    $stream.Seek($peHeaderOffset + 4, [System.IO.SeekOrigin]::Begin) | Out-Null
    $machine = $reader.ReadUInt16()

    $reader.Close()
    $stream.Close()

    return [PSCustomObject]@{
        Machine = $machine
    }
}

# Define the directory to search
$directory = "E:\Utils"

# Get all .exe files in the directory
$exeFiles = Get-ChildItem -Path $directory -Filter *.exe

# Filter out 32-bit executables
# Filter out 64-bit executables
$exeFiles32Bit = $exeFiles | Where-Object {
    $file = $_.FullName
    $binaryType = (Get-PEHdr -Path $file).Machine
    $binaryType -eq 0x014c  # 0x014c is the identifier for 32-bit executables
#    $binaryType -eq 0x8664  # 0x8664 is the identifier for 64-bit executables
}

# Output the 32-bit executables
$exeFiles32Bit | Select-Object FullName

The powershell solution is much faster than the .BTM solution that I was working on,
and Rex said that adding this ability to the DIR command would have a substantial performance penalty.

I will update when I've had more time to test this out.

Joe
 
It is a very bad idea to read file contents for simple directory listing.
Especially bad for virtual filesystems (like, cloud ones).
 
Back
Top