PowerShell Disks and Partitions Management. There are about 166 (Windows 1903) PowerShell cmdlets in the Storage module in Windows 10. To display all available commands related to disk management.
Get-Command -Module Storage
List Local Disks and Partitions
Display the list of local disks available in your system at the logical level.
Get-Disk | ft -AutoSize
Select only the system disk on which Windows is installed.
Get-Disk | Where-Object IsSystem -eq $True | fl
You can display Offline disks only.
Get-Disk | Where-Object IsOffline –Eq $True| ft –AutoSize
If you need information about physical disks (the characteristics and status of physical disks on a computer), use Get-PhysicalDisk.
You can display the list of partitions on all disks.
Get-Partition
Or partitions on the specified disks only.
Get-Partition –DiskNumber 1,2
Disk Initialization in PowerShell
1. Get the disk Online;
Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline $False
2. Initialize it;
Initialize-Disk -Number 1
By default, a GPT (GUID) partition table is created on a disk, but if you need an MBR one.
Initialize-Disk 1 –PartitionStyle MBR
NOTE: If there are some data on the disk, you can change the partition table from MBR to GPT without removing the data using the mbr2gpt.exe tool.
How to Create Partitions on a Disk?
To create a new partition on a disk, the New-Partition cmdlet is used.
New-Partition –DiskNumber 1 -Size 100gb -DriveLetter G
If you want the partition to occupy all available disk space, use the UseMaximumSize attribute. To assign a letter automatically, the AssignDriveLetter parameter is used.
New-Partition –DiskNumber 1 -AssignDriveLetter –UseMaximumSize
You can change the assigned letter.
Set-Partition –DriveLetter L -NewDriveLetter U
If you want to make a partition active.
Set-Partition -DriveLetter U -IsActive $true
Formatting a partition with PowerShell
Format the new partition in the NTFS and set the Data volume label.
Format-Volume -DriveLetter G -FileSystem NTFS -NewFileSystemLabel Data -Confirm:$false
How to Remove Partitions from a Disk?
Remove all partitions on disks 1 and 2 without confirmation.
Get-Partition –DiskNumber 1,2 | Remove-Partition -Confirm:$false
To delete all partitions from disks and completely clear data.
Clear-Disk -Number 1 -RemoveData -Confirm:$false