, , ,

Replace Diskpart with Windows PowerShell – Basic Storage cmdlets

Windows Server 8

Last week I made a blog post about how you can create a USB drive for Windows To Go. In my post I used diskpart.exe to format the USB drive. Now we don’t live in the stone age anymore, so I did the same with the new version of Windows PowerShell coming in Windows 8 and Windows Server 8.

Now here some basic cmdlets to do some simple storage operations, like clean a disk, create a partition and so on.

Lets start simple

List all disks

Get-Disk

get-disk

Now get all partitions

Get-Partition

get-partition

Now get all partitions of disk 0

Get-Partition -DiskNumber 0

get-partitiondisk

Clear a Disk

Get-Disk 1 | Clear-Disk -RemoveData

clear-disk

Create a new partition

New-Partition -DiskNumber 1 -UseMaximumSize

new-partition

Format this volume

Get-Partition -DiskNumber 1 -PartitionNumber 1 | Format-Volume -FileSystem NTFS

format-volume

Create new partition and format it with the label “USB”:

New-Partition -DiskNumber 1 -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel USB

format-volume1

Change driveletter

Set-Partition -DriveLetter E -NewDriveLetter T

set-partition

Set a partition active

Set-Partition -DriveLetter T -IsActive $true

set-partition1

Remove a partition

Remove-Partition -DriveLetter T

remove-partition

Bring a disk online

 Set-Disk 1 -isOffline $false 

Remove Readonly flag

 Set-Disk 1 -isReadOnly $false 

Initialize Disk with GPT

 Initialize-Disk 1 -PartitionStyle GPT 

online disk

This is some basic knowledge about the storage module in PowerShell v3. Lets see how we can change the commands from using diskpart to Windows PowerShell.

Diskpart.exe

select disk 1
clean
create partition primary
format fs=ntfs quick
active
assign letter=e

PowerShell:

Get-Disk 1 | Clear-Disk -RemoveData
New-Partition -DiskNumber 1 -UseMaximumSize -IsActive -DriveLetter E | Format-Volume -FileSystem NTFS -NewFileSystemLabel USB

replace-diskpart

13 responses

  1. Lee Binder Avatar
    Lee Binder

    hoi Thomas. Just tried this w. the Win 7 (64) powershell – no go, does not know these commands .. Seems MS vamped up the Win 8 PS w. a bunch of new commands. Do you know if there’s a way to upgrade the Win 7 PS to the level of the one from Win8?

    1. Thomas Maurer Avatar

      There is a CTP of PowerShell 3.0 for Windows 7, but I am not sure if this will include the Storage or Network cmdlets

  2. Carlos Perez Avatar

    Can one also extend the FS on a partition like with Diskpart? That has been the number 1 use I have had for that command when expanding boot volumes or SAN connected volumes to allocate more space.

    1. Thomas Maurer Avatar

      Yes there is a resize-partition cmdlet :)

  3. Philipp Avatar
    Philipp

    Thomas, vielen Dank, bist einfach nur leiwand ;)
    lg aus Ö

  4. Joe Gasper Avatar
    Joe Gasper

    And you can work with mount points, too.

    $myDisk = “2”
    $myLabel = “Bin”
    $myMount = “M:\Bin”
    Get-Disk $myDisk | Set-Disk -IsOffline $false
    Get-Disk $myDisk |Initialize-Disk -PartitionStyle GPT
    Get-Disk $myDisk | New-Partition -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel $myLabel -AllocationUnitSize 65536 -Confirm:$false
    Add-PartitionAccessPath -DiskNumber $myDisk -PartitionNumber 2 -AccessPath $myMount

  5. craig Avatar

    how about replacing diskpart “create vdisk”? I see there is a “New-VirtualDisk” but that does not require/use a path and .vhd name.

    1. Thomas Maurer Avatar

      The New-VirtualDisk cmdlet is not used for VHD or VHDX files. The New-VirtualDisk cmdlets is used to create new Storage Spaces (Virtual Disk) on a Storage Pool. The command you are looking for is New-VHD :)

  6. craig Avatar

    Thanks Tom. I saw the VHD cmdlets and thought they were for VMs only but now I get it. In case it helps someone else, here is what I have, 2 lines . Together they create the vhd, mount, initialize, and format.

    New-vhd c:\vhdtest.vhd –SizeBytes 130GB -Dynamic

    Mount-VHD –Path c:\vhdtest.vhd -PassThru | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -UseMaximumSize -DriveLetter x | Format-Volume -FileSystem NTFS -NewFileSystemLabel testlabel -confirm:$false

    #Dismount-VHD –Path c:\vhdtest.vhd ## to run if you are playing and want to unmounnt and delete to try again.

  7. Xmblpb Avatar

    Just vanished my partition scheme with Powers Hell.

    Only set active windows partition and this immediately delete two non-windows partitions.
    This was never happens when i’ve set active partition with Disk Management.

  8. Adam Conkle - MSFT Avatar
    Adam Conkle – MSFT

    DiskPart.exe – Powershell Parser for Windows Server 2008 R2
    https://gallery.technet.microsoft.com/DiskPartexe-Powershell-0f7a1bab

  9. Vlad Avatar

    In part “Set a partition active”, change $ture for $true.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.