Powershell - Managing disks, partitions and volumes

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello, A new article on how to manage disks, partitions and volumes in Powershell.

# List the commands for managing disks
Get-Command -Module Storage
## Disk
# List disks
Get-Disk
# List data disks
Get-Disk |
Where-Object IsSystem -Eq $False
# List offline disks
Get-Disk |
Where-Object IsOffline -Eq $True
# List USB-connected disks
Get-Disk |
Where-Object -Property BusType -eq USB
# List bootable disks
Get-Disk |
Where-Object -Property IsBoot -eq $true
# Display the serial number, model and firmware version of the disks
Get-Disk | Select-Object -Property FriendlyName,SerialNumber,Model,FirmwareVersion
# Bring all offline disks online
Get-Disk |
Where-Object IsOffline -Eq $True |
Set-Disk -IsOffline $False
# Initialise uninitialised disks in GPT
Get-Disk |
where-object PartitionStyle -eq 'raw' |
Initialize-Disk -PartitionStyle GPT
##Partition
# List the partitions on a disk
Get-Partition -DiskNumber 0
# List all partitions
Get-Disk |
Get-Partition |
Select-Object -Property *
# Display partition information by drive letter
Get-Partition -DriveLetter C
# Display partition information by partition number
Get-Partition -PartitionNumber 4
## Partition
# Create a new 30 GB partition with an automatically defined drive letter
New-Partition -DiskNumber 1 -Size 30GB -AssignDriveLetter
# Create a new partition with drive letter G using all remaining disk space
New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter G
# Remove partitions from a disk
Get-Partition -DiskNumber 1 |
Remove-Partition -Confirm:$false
# Modify the size of a partition (cannot be less than the disk space used and greater than the size of the disk)
$SupportedSize = Get-PartitionSupportedSize -DriveLetter F
Resize-Partition -DriveLetter F -Size $SupportedSize.SizeMax
# Change the size of a partition to a defined size
Resize-Partition -DriveLetter F -Size 25GB
# Change the letter of a partition
Set-Partition -DriveLetter F -NewDriveLetter H
# Remove a specific partition without requesting confirmation
Remove-Partition -DiskNumber 1 -PartitionNumber 1 -Confirm:$false
# Delete all the partitions on a disk without prompting for confirmation
Clear-Disk -Number 1 -Confirm:$False
# Delete data and OEM partitions without prompting for confirmation
Clear-Disk -FriendlyName usbkey -RemoveData -RemoveOEM -Confirm:$False
## Volume
# Format the volume in NTFS (or FAT, FAT32, exFat, ReFS) and give it a name without asking for confirmation
Format-Volume -DriveLetter F -FileSystem NTFS -NewFileSystemLabel 'Data' -Confirm:$false
# List all volumes
Get-Volume
# Display a specific volume using its drive letter
Get-Volume -DriveLetter c
# List volumes that require maintenance
get-volume |
Where-Object -Property HealthStatus -NE healthy
# List removable volumes
get-volume |
Where-Object -Property DriveType -EQ Removable
# Display disk space information
Get-CimInstance Win32_LogicalDisk |
select-object DeviceId, VolumeName,
@{n='TotalSize (GB)';e={[math]::Round($_.Size/1GB,2)}},
@{n='UsedSpace (GB)';e={[math]::Round($_.Size/1GB - $_.FreeSpace/1GB,2)}},
@{n='FreeSpace (GB)';e={[math]::Round($_.FreeSpace/1GB,2)}}
# or
Get-Volume |
Select-Object -Property DriveLetter,FriendlyName,size,sizeremaining
# Display the number of NTFS file system errors on a volume
Get-VolumeCorruptionCount -DriveLetter D
# Initialise, partition and format
Get-Disk |
Where-Object PartitionStyle -eq 'raw' |
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -AssignDriveLetter -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel 'Data'
# Clean and reformat a USB key
Clear-Disk -FriendlyName 'usbkey' -RemoveData -RemoveOEM -Confirm:$False -PassThru |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -AssignDriveLetter -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel 'USBDATA'

Related links