tags:

views:

39

answers:

2

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

A: 

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.

-Oisin

x0n
I was looking at WMI or .net library calls. I will see about parsing the output of diskpart.exe
Josh
take a look at the output of: gwmi win32_partition | % { $_ | fl * }
x0n
+2  A: 

Using WMI

gwmi -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 "*"}}
Josh