How to determine if WinPE (4) booted from UEFI or BIOS?

I'm looking for a reliable detection method when I boot into WinPE 4 (powershell) (or WinPE 3 (vbs) as an alternative), did I boot from UEFI or BIOS System? (without starting a third-party exe, as I am in a limited environment)

This significantly changes the way you partition Windows deployments as you change and format partition layouts. (GPT vs MBR, etc.)

I have one job that is an adaptation of this C ++ code in powershell v3, but it looks pretty hack:

## Check if we can get a dummy flag from the UEFI via the Kernel ## [Bool] check the result of the kernel 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() } 

It looks pretty simple to get a simple flag.

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

+7
source share
5 answers

It is no less hacked, in the sense that it will still require interaction from powershell, but the interaction code can be more accurate if you use (or can call): GetFirmwareType() .

This returns the FIRMWARE_TYPE enumeration registered here . I can’t believe that both functions are provided in Windows 8 and exported kernel32.dll, which Microsoft has documentation pointing to "using a dummy variable"!

Internally, GetFirmwareType calls NtQuerySystemInformation . I will delve into what he does, but I do not think that this will necessarily be enlightenment.

Unfortunately, this only works for PE4 (Windows 8), as these features were added only then.

+1
source

It may be a little late, but if it is known that they work in WinPE, the following code should work:

 $isuefi = (Get-ItemProperty -Path HKLM:\System\CurrentControlSet\Control).PEFirmwareType -eq 2 
+1
source

I don’t have a UEFI system to test it, but this article seems to imply a registry value there that you can query under PE to indicate in which mode it was loaded:

http://technet.microsoft.com/en-us/library/dn293283.aspx

0
source

It looks like the PE environment has a folder specific to the PE environment. In addition, the% TargetDir% variable, the TARGETDIR property , is described here.

Finally, you can check if you are running from X: there should also be a folder with the boot.wim file, which you can check. I believe the path will be X: \ Sources \ Boot.wim , but double check.

 if ( Test-Path "%TargetDir%\Windows\wpeprofiles" ) { Write-host "You're in Windows PE" } 
-one
source

I don't know if this will help (based on a C # solution), but:

Win32_DiskPartition has the properties "Boot" (bool), "BootPartition" (bool) and "Type" (string). For my UEFI system, "Type" is returned as the string "GPT: System".

Now, for all bootable Win32_DiskPartitions boot files, they are the boot partition and have the specified type, determine if any of them are internal.

Hope this helps.

-one
source

All Articles