Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if WinPE(4) has booted from a UEFI or BIOS?

Tags:

People also ask

How can I tell if WinPE is UEFI or BIOS?

Detect if WinPE is booted into BIOS or UEFI Modeif %Firmware%==0x1 echo The PC is booted in BIOS mode. if %Firmware%==0x2 echo The PC is booted in UEFI mode. Note that between delims= and " %%A is a tab, followed by a space.

How do I know if my Windows is booted in UEFI or Legacy BIOS?

Click the Search icon on the Taskbar and type in msinfo32 , then press Enter. System Information window will open. Click on the System Summary item. Then locate BIOS Mode and check the type of BIOS, Legacy or UEFI.


I am looking for a way to reliably detect when I boot into WinPE 4 (powershell) (or WinPE 3 (vbs) as an alternative), have I booted from a UEFI or BIOS System? (without running a third party exe as I am in a restricted environment)

This significantly changes how I would be partitioning a windows deployment as the partitions layout changes and format. (GPT vs. MBR, etc)

I have one working that is an adaptation of this C++ code in powershell v3 but it feels pretty hack-ish :

## Check if we can get a dummy flag from the UEFI via the Kernel
## [Bool] check the result of the kernel's fetch of the dummy GUID from UEFI
## The only way I found to do it was using the C++ compiler in powershell
Function Compile-UEFIDectectionClass{
    $win32UEFICode= @'
    using System;
    using System.Runtime.InteropServices;

    public class UEFI
    {
       [DllImport("kernel32.dll")]
       public static extern UInt32 GetFirmwareEnvironmentVariableA([MarshalAs(UnmanagedType.LPWStr)] string lpName, [MarshalAs(UnmanagedType.LPWStr)] string lpGuid, IntPtr pBuffer, UInt32 nSize); 

       public static UInt32 Detect()
       {
            return GetFirmwareEnvironmentVariableA("", "{00000000-0000-0000-0000-000000000000}", IntPtr.Zero, 0);
       }
    }
    '@

Add-Type $win32UEFICode
}


## A Function added just to check if the assembly for 
## UEFI is loaded as is the name of the class above in C++.
Function Check-IsUEFIClassLoaded{
     return ([System.AppDomain]::CurrentDomain.GetAssemblies() | % { $_.GetTypes()} | ? {$_.FullName -eq "UEFI"}).Count 
}

## Just incase someone was to call my code without running the Compiled code run first
If (!(Check-IsUEFIClassLoaded)){
    Compile-UEFIDectectionClass
}

## The meat of the checking.
## Returns 0 or 1 ([BOOL] if UEFI or not)
Function Get-UEFI{
    return [UEFI]::Detect()
}

This seems pretty over the top just to get a simple flag.

Does anyone know if there is there a better way to get this done?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!