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