Detect GPT and MBR partitions with Powershell
Asked Answered
C

4

6

Is there a way to tell if a disk has a GPT or an MBR partition with powershell?

Chansoo answered 1/10, 2010 at 15:54 Comment(0)
C
8

Using WMI

Get-WmiObject -Query "Select * from Win32_DiskPartition WHERE Index = 0" |
  Select-Object DiskIndex, @{
    Name = "GPT";
    Expression = {$_.Type.StartsWith("GPT")}
  }

Using Diskpart

$a = "list disk" | diskpart

$m = [String]::Join("`n", $a) |
  Select-String -Pattern "Disk (\d+).{43}(.)" -AllMatches

$m.Matches |
  Select-Object @{
    Name = "DiskIndex";
    Expression = {$_.Groups[1].Value}}, @{
    Name = "GPT";
    Expression = {$_.Groups[2].Value -eq "*"}
  }

Chansoo answered 1/10, 2010 at 19:37 Comment(0)
F
10

If you are on Windows 8, Windows Server 2012, or newer, then you can use one of the storage cmdlets to check this:

Get-Disk

The output of this command will be formatted like:

PS C:\> Get-Disk

Number Friendly Name                            OperationalStatus                    Total Size Partition Style
------ -------------                            -----------------                    ---------- ---------------
0      Microsoft Virtual Disk                   Online                                    42 GB GPT
1      Microsoft Virtual Disk                   Online                                     1 GB GPT
2      Microsoft Virtual Disk                   Offline                                    2 GB RAW
3      Microsoft Virtual Disk                   Offline                                    3 GB RAW

Notice that the rightmost column indicates the Partition Style, which is the piece of data that you are seeking.

If you are on Windows 7, Windows Server 2008 R2, or older, then you should use diskpart or WMI to get this information. I prefer to use diskpart. Type

diskpart

followed by

list disk

The output will look like:

PS C:\> diskpart

Microsoft DiskPart version 6.3.9600

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: WIN-BN8G3VMNQ9T

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           42 GB      0 B        *
  Disk 1    Online         1024 MB   991 MB        *
  Disk 2    Offline        2048 MB  2048 MB
  Disk 3    Offline        3072 MB  3072 MB

Note that Disk 0 and 1 are both GPT disks, and they have an asterisk in the appropriate column.

Furmark answered 29/2, 2016 at 2:35 Comment(1)
in an expression: (Get-Disk -Number 0).PartitionStyle -eq 'MBR', (Get-Disk -Number 0).PartitionStyle -eq 'GPT'Haynie
C
8

Using WMI

Get-WmiObject -Query "Select * from Win32_DiskPartition WHERE Index = 0" |
  Select-Object DiskIndex, @{
    Name = "GPT";
    Expression = {$_.Type.StartsWith("GPT")}
  }

Using Diskpart

$a = "list disk" | diskpart

$m = [String]::Join("`n", $a) |
  Select-String -Pattern "Disk (\d+).{43}(.)" -AllMatches

$m.Matches |
  Select-Object @{
    Name = "DiskIndex";
    Expression = {$_.Groups[1].Value}}, @{
    Name = "GPT";
    Expression = {$_.Groups[2].Value -eq "*"}
  }

Chansoo answered 1/10, 2010 at 19:37 Comment(0)
O
0

To find out if any disk has a MBR or a GPT this is very easy.. Start powershell. Run.. diskpart , press ENTER, run.. list disk , press ENTER. Here is my computer's output:

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          232 GB  1024 KB
Disk 1    Online          465 GB  1024 KB
Disk 2    Online         3820 MB      0 B

You will get a yes entry, I think!, for Dyn or Gpt if your disk is dynamic or the partition table type is Gpt respectively. I searched for some time and enough is enough for my purposes. To compare the output of the script by Josh using WMI, here is the output..

DiskIndex                                     GPT
---------                                     ---
        0                                   False
        1                                   False
        2                                   False
Orlando answered 22/11, 2011 at 18:10 Comment(1)
If you have GPT, then your output on Windows 10 Powershell 5.x will look like: DISKPART> list disk Disk ### Status Size Free Dyn Gpt -------- ------------- ------- ------- --- --- Disk 0 Online 238 GB 0 B *Shipe
U
-2

No. PowerShell does not have any native built-in commands for this. PowerShell, as the name suggests, is a shell. It comes with a good set of useful, generic cmdlets but specialization like this is left to external native commands (like diskpart), modules and/or snapins.

Since you're always going to find diskpart.exe where you find powershell, use that.

If you're intent on using PowerShell alone, then perhaps WMI could help. Take a look at the output of:

PS> gwmi win32_partition | % { $_ | fl * }

-Oisin

Uproot answered 1/10, 2010 at 18:21 Comment(4)
I was looking at WMI or .net library calls. I will see about parsing the output of diskpart.exeChansoo
take a look at the output of: gwmi win32_partition | % { $_ | fl * }Uproot
I think this answer is technically correct, but it misses the purpose of the question. I would suggest incorporating your comment into your answer.Hunkers
This answer is no longer technically correct, because PowerShell has Get-Disk as of Windows 8 / Windows Server 2012.Furmark

© 2022 - 2024 — McMap. All rights reserved.