Powershell diskpart help needed

Hello All.

Need help creating a powershell script that will

List all volume names on a given machine
List what disk the volume belongs to and if the disk is MBR or GPT

and if possible also list the lun id of the disk.


To highlight a little better what I am aiming for is the following

From list disk

Disk ###  Status      Size     Free     Dyn  Gpt
--------  ----------  -------  -------  ---  ---
Disk 0    Online       279 GB      0 B
Disk 1    Reserved      11 GB      0 B
Disk 2    Reserved     501 GB      0 B
Disk 3    Reserved    2049 GB      0 B        *
Disk 4    Reserved    1002 GB      0 B        *
Disk 5    Reserved    1003 GB      0 B        *

There is a star if the disk is GTP and no star if MBR.  I need to know if a disk is MBR or GPT

Now if you do a select disk and detail disk you get following output


DISKPART> select disk 36

Disk 36 is now the selected disk.

DISKPART> detail disk

VV  Multi-Path Disk Device
Disk ID: {4FD8438C-49C3-409C-8671-E167DAC489A5}
Type   : FIBRE
Bus    : 0
Target : 1
LUN ID : 36
Read-only  : No
Boot Disk  : No
Pagefile Disk  : No
Hibernation File Disk  : No
Crashdump Disk  : No

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 3     I   SQL Backups  NTFS   Partition   4096 GB  Healthy


The output shows the volume name (Label), the LUN ID

So what I am mainly looking for is a way to get a list of disks.

The disk number

 If the disk is GPT or MBR.

Volume number

The name of the volume on the disk

The lun ID of the volume
Aria2084Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nadav SolomonCommented:
$disks = @()
gwmi -query "Select * from Win32_DiskPartition" | %{
    $gpt = ""
    if($_.Type.StartsWith("GPT"))
    {
        $gpt = "*"
    }
    $disk = New-Object -TypeName psObject -Property @{
        Name = $_.Name
        Size = $_.Size
        GPT = $gpt
    }
    $disks += $disk
}
$disks|ft name,size,gpt -AutoSize

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Almost, Nadav. It works, but if using a pipe, you don't collect data that way. Just pass it thru:
gwmi -query "Select * from Win32_DiskPartition" | %{
    $gpt = ""
    if($_.Type.StartsWith("GPT"))
    {
        $gpt = "*"
    }
    New-Object -TypeName psObject -Property @{
        Name = $_.Name
        Size = $_.Size
        GPT = $gpt
    }
} | ft name,size,gpt -AutoSize

Open in new window

If there is any need to keep the results in a var, the proper way is to replace the last line with
} | tee -var disks | ft name,size,gpt -AutoSize

Open in new window

This change is a bit tricky, and a matter of taste:
gwmi -query "Select Name, Size, Type from Win32_DiskPartition" | %{
    New-Object -TypeName psObject -Property @{
        Name = $_.Name
        Size = $_.Size
        GPT = "*" * $_.Type.StartsWith("GPT")
    }
} | ft name,size,gpt -AutoSize

Open in new window

or (combining two further changes)
gwmi Win32_DiskPartition -Property Name, Size, Type |
  Select Name, Size, @{n='GPT'; e={ "*" * $_.Type.StartsWith("GPT")} |
  ft -auto Name, Size, GPT

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.