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
Now get all partitions
Get-Partition
Now get all partitions of disk 0
Get-Partition -DiskNumber 0
Clear a Disk
Get-Disk 1 | Clear-Disk -RemoveData
Create a new partition
New-Partition -DiskNumber 1 -UseMaximumSize
Format this volume
Get-Partition -DiskNumber 1 -PartitionNumber 1 | Format-Volume -FileSystem NTFS
Create new partition and format it with the label “USB”:
New-Partition -DiskNumber 1 -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel USB
Change driveletter
Set-Partition -DriveLetter E -NewDriveLetter T
Set a partition active
Set-Partition -DriveLetter T -IsActive $true
Remove a partition
Remove-Partition -DriveLetter T
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
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
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?
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
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.
Yes there is a resize-partition cmdlet :)
Thomas, vielen Dank, bist einfach nur leiwand ;)
lg aus Ö
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
how about replacing diskpart “create vdisk”? I see there is a “New-VirtualDisk” but that does not require/use a path and .vhd name.
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 :)
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.
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.
DiskPart.exe – Powershell Parser for Windows Server 2008 R2
https://gallery.technet.microsoft.com/DiskPartexe-Powershell-0f7a1bab
In part “Set a partition active”, change $ture for $true.
uf thanks :)