Is partition GPT or MBR?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
YottaByte
Posts: 13
Joined: 14 Oct 2020 13:07
Location: Argentina

Is partition GPT or MBR?

#1 Post by YottaByte » 02 Aug 2021 05:59

Hello everybody.

To know what type of partition the system disk has, I did this batch:

Code: Select all

@ECHO OFF
CLS
DISKPART /s diskpart.txt >disk.txt
FIND "*" "C:\Users\Eduar\Documents\Bloc de notas\BATs\disk.txt" >NUL
IF %ERRORLEVEL% == 0 (SET "Part=Particion GPT") ELSE (SET "Part=Particion MBR")
ECHO %Part%
PAUSE >NUL
EXIT
The Diskpart.txt file contains:

Code: Select all

list disk
The Disk.txt file contains:

Code: Select all

  N£m Disco  Estado      Tama¤o   Disp     Din  Gpt
  ---------- ----------  -------  -------  ---  ---
  Disco 0    En l¡nea        931 GB  1024 KB        *
  Disco 1    En l¡nea         14 GB      0 B         
  Disco 2    En l¡nea         14 GB  7168 KB         
But I need "Disk.txt" to show only system disk information (%SystemDrive%).
How can I make diskpart show that information only from %SystemDrive%?
This way:

Code: Select all

  N£m Disco  Estado      Tama¤o   Disp     Din  Gpt
  ---------- ----------  -------  -------  ---  ---
  Disco 0    En l¡nea        931 GB  1024 KB        *
Thanks in advance.

Eduardo

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Is partition GPT or MBR?

#2 Post by atfon » 02 Aug 2021 06:58

If you don't mind a powershell solution, you could utilize something like this:

Code: Select all

%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe /command "get-disk | where BootFromDisk -eq "True" | select PartitionStyle"
On my system, I can also capture some information about GPT from wmic:

Code: Select all

wmic partition where "bootPartition='true'" get type /value

YottaByte
Posts: 13
Joined: 14 Oct 2020 13:07
Location: Argentina

Re: Is partition GPT or MBR?

#3 Post by YottaByte » 02 Aug 2021 08:27

atfon wrote:
02 Aug 2021 06:58
If you don't mind a powershell solution, you could utilize something like this:

Code: Select all

%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe /command "get-disk | where BootFromDisk -eq "True" | select PartitionStyle"
On my system, I can also capture some information about GPT from wmic:

Code: Select all

wmic partition where "bootPartition='true'" get type /value
Thank you very much atfon! I will use wmic.

Eduardo

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Is partition GPT or MBR?

#4 Post by atfon » 02 Aug 2021 14:09

You could also combine wmic with your code. You could get the disk index as a variable with this command:

Code: Select all

for /F "tokens=2 delims==" %%e in ('wmic partition where "bootPartition='true'" get diskindex /value ^|findstr /r [0-9]') do for /f "tokens=1" %%f in ("%%e") do set "dIndex=%%f"
You can then type the contents of disk.txt and pipe findstr with the variable to parse the contents like this:

Code: Select all

type "C:\Users\Eduar\Documents\Bloc de notas\BATs\disk.txt" |findstr /r /c "Disk %dIndex%" | findstr \*
You could generate the contents of the diskpart.txt by just echoing the text you want to the disk.txt file:

Code: Select all

echo list disk > "C:\Users\Eduar\Documents\Bloc de notas\BATs\diskpart.txt"
You may also want to delete the two text files at the end of your script.

Edit: I updated the search string above. Using findstr \* will search for the asterisk [*] character.

YottaByte
Posts: 13
Joined: 14 Oct 2020 13:07
Location: Argentina

Re: Is partition GPT or MBR?

#5 Post by YottaByte » 03 Aug 2021 14:55

Using WMIC it looked like this:

Code: Select all

@ECHO OFF
CLS
	SETLOCAL EnableExtensions
	SETLOCAL EnableDelayedExpansion
		
	MODE CON COLS=52 LINES=8
	TITLE TipoPart
		COLOR 9B
		
	WMIC PARTITION WHERE "BootPartition='True'" GET TYPE /VALUE >"%~dp0Disk.txt"
	FIND "GPT" "%~dp0Disk.txt" >NUL
	IF "%ERRORLEVEL%"=="0" (SET "Part=GPT") ELSE (SET "Part=MBR")

CLS
ECHO  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
ECHO  ³  TIPO DE TABLA DE PARTICIàN DISCO DE SISTEMA   ³
echo  ÃÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ´
ECHO  ³Tabla de Partici¢n de "%SystemDrive%" es: %Part%              ³
ECHO  ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´
ECHO  ³²²²²²²²²²²²²²²²²² (C)Ä^> Cerrar ²²²²²²²²²²²²²²²²²³
ECHO  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

	CHOICE /c c /n
	IF ERRORLEVEL 1 GOTO SALE
	
:SALE
IF EXIST "%~dp0Disk.txt" (DEL "%~dp0Disk.txt")
EXIT
Eduardo

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Is partition GPT or MBR?

#6 Post by atfon » 26 Oct 2021 09:44

Digging around WMI, I found a simpler way to do this without relying on exporting to an external file. The MSFT_Disk Class contains PartitionStyle that can determine if you have MBR, GPT or Unknown.

https://docs.microsoft.com/en-us/previo ... /msft-disk

Code: Select all

for /f "skip=1 tokens=2 delims=," %%g in ('wmic /NameSpace:\\ROOT\Microsoft\Windows\Storage Path MSFT_Disk where "BootFromDisk='true'" get PartitionStyle /format:csv') do for /f "tokens=*" %%h in ("%%g") do (
if "%%h" EQU "0" (set "partType=Unknown") else if "%%h" EQU "1" (set "partType=MBR") else if "%%h" EQU "2" (set "partType=GPT")
)
echo Partition Type: %partType%

Post Reply