Search code examples
powershellmbrguid-partition-table

Detect GPT and MBR partitions with Powershell


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


Solution

  • 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 "*"}
      }