Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Disks and Partitions Management with PowerShell on Windows

July 11, 2025

Disks and Partitions Management with PowerShell on Windows

This post will cover how to manage disks, partitions, and volumes using the PowerShell command line. All disk and partition management tasks that are typically performed using the Disk Management GUI (diskmgmt.msc) or the DiskPart.exe command-line tool can also be executed from PowerShell. We’ll explain how to retrieve information about connected physical disks, initialize a disk, create a partition table, create a new partition, format it, and assign a drive letter.

Contents:
  • PowerShell: List Local Disks and Partitions
  • Disk Initialization in PowerShell
  • How to Create Partitions on a Disk Using PowerShell
  • Format-Volume
  • Removing Partitions from a Disk with PowerShell

The built-in Storage module (available starting with PowerShell 3.0 and newer) contains over 170 cmdlets for managing disks and partitions.

Get-Command -Module Storage

local storage (disk, partition)management module in windows powershell

Important.  Be careful not to accidentally delete or format a partition containing data when managing disks, partitions, and tables in the PowerShell CLI.

PowerShell: List Local Disks and Partitions

First, let’s list the available local disks on a computer at the logical level.

Get-Disk | ft -AutoSize

To select only the system drive on which Windows is installed, run the following command:

Get-Disk | Where-Object IsSystem -eq $True | fl

This command displays all attributes of the system disk. These attributes can be used to select disks based on different criteria.

UniqueId : SCSI\DISK&VEN_VMWARE&PROD_VIRTUAL_DISK\5&1EC51BF7&0&000000:DESKTOP-JOPF9
Number : 0
Path : \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&1ec42ba7&0&000000#{21f23456-a6bf-12d0-94f2-001efb8b}
Manufacturer : VMware
Model : Virtual disk
SerialNumber :
Size : 98 GB
AllocatedSize : 98432321434
LogicalSectorSize : 512
PhysicalSectorSize : 512
NumberOfPartitions : 2
PartitionStyle : MBR
IsReadOnly : False
IsSystem : True
IsBoot : True

Print the disk name, serial number, model, and firmware version:

Get-Disk | Select-Object -Property FriendlyName, SerialNumber,Model,FirmwareVersion

Filter only by disks with an Offline status.

Get-Disk | Where-Object IsOffline –Eq $True| ft –AutoSize

powershell Get-Disk offline and system

Use the Get-PhysicalDisk cmdlet to view the characteristics and status of local physical disks on a computer.

Get-PhysicalDisk info from powershell

We previously explained how to use the Get-PhysicalDisk command to configure Storage Spaces Direct (S2D) volumes and to detect a failed physical disk.

With PowerShell, you can detect whether a drive is an SSD or HDD, or if it is a USB flash drive (Unspecified media type).

DeviceId Model                      MediaType   BusType         Size

——— ——                      ———   ——-         —-

0        TOSHIBA MK3775VSXP         HDD         SATA    500156374016

1        Samsung SSD 840 PRO Series SSD         SATA    128060514304

2        Transcend                 UnSpecified USB     128169757184

For example, to display all connected USB drives:

Get-Disk | Where-Object -Property BusType -eq USB

The disk health can be checked from PowerShell by looking at the HealthStatus attribute, as well as the SMART values of the disk.

Get-PhysicalDisk | Get-StorageReliabilityCounter | Select-Object DeviceId, Temperature, PowerOnHours, LoadUnloadCycleCount, ReadErrorsTotal, WriteErrorsTotal | Format-Table -AutoSize

check physical disk health and smart values powershell

If you need to put the disk into read-only mode (this only applies to the current computer):

Set-Disk 2 -IsReadOnly 1

Disable read-only mode:

Set-Disk 2 -IsReadOnly 0

List the partitions on all the disks in a computer.

Get-Partition

Or get partitions on the specified disks only:

Get-Partition –DiskNumber 1,2

List all logical volumes on a Windows computer:

Get-Volume

Get-Volume cmdlet

Note that disk numbering starts at 0, while partition numbering starts at 1. 

Disk Initialization in PowerShell

In the previous example, one of the computer’s disks was Offline and had a RAW partition table (in the Partition Style column). This is a new, clean disk that has just been connected to the computer. Let’s try to initialize it, create a GPT or MBR partition table, and create a new partition on it.

In order for the new disk to be available to the operating system, it must first be brought Online.

Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline $False

Now you can initialize this disk (its index is 1):

Initialize-Disk -Number 1

Get-Disk online and Initialize-Disk in powershell

The disk is formatted with a GPT (GUID) partition table by default. If you want to change the partition table type to MBR, run the following command:

Set-Disk 2 -PartitionStyle MBR

The built-in mbr2gpt.exe tool can be used to convert the partition table from MBR to GPT without deleting any partition or data.

To avoid specifying the disk number, you can initialize all connected disks with the RAW partition table:

Get-Disk | Where-Object PartitionStyle –Eq 'RAW' | Initialize-Disk

 Note that the disk may have the RAW status if the partition table is corrupted. Try using the free TestDisk tool to recover the partition table and data on your RAW disk.

How to Create Partitions on a Disk Using PowerShell

The New-Partition cmdlet is used to create a new partition on a disk. Let’s create a 10 GB partition and assign the letter L: to it:

New-Partition –DiskNumber 1 -Size 10gb -DriveLetter L

create disk partition with powershell

If you want the partition to occupy all available disk space, use the UseMaximumSize attribute. To automatically assign a drive letter, use the AssignDriveLetter option (sometimes Windows doesn’t assign a drive letter automatically).

New-Partition –DiskNumber 1 -AssignDriveLetter –UseMaximumSize

To change the drive letter assigned to a partition, use the command:

Set-Partition –DriveLetter L -NewDriveLetter U

If there is any unused space, an existing partition on the disk can be extended using it. First, check the maximum available partition size (SizeMax). Then, increase the partition size to its maximum using unallocated space.

$MaxSize = (Get-PartitionSupportedSize -DriveLetter L).SizeMax
Resize-Partition -DriveLetter L -Size $MaxSize

Resize-Partition from posh

In Windows, it is not possible to extend a volume located to the right (after) of unallocated space. 

This command is used to make a specific partition active.

Set-Partition -DriveLetter U -IsActive $true

Display information about the used and free space on the logical volumes.

Get-CimInstance -Class Win32_LogicalDisk |
Select-Object -Property DeviceID, VolumeName, @{Label='FreeSpace (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{label='FreePercent'; expression={[Math]::Round(($_.freespace / $_.size) * 100, 2)}}|ft

free and used space on volumes check via powershell

If you need to set a specific partition to ReadOnly mode:

Set-Partition -DiskNumber 2 -PartitionNumber 2 -IsReadOnly 1

Format-Volume

Let’s format a new partition in the NTFS file system and assign it the DBData volume label:

Format-Volume -DriveLetter L -FileSystem NTFS -NewFileSystemLabel DBData -Confirm:$false

Format-Volume

Removing Partitions from a Disk with PowerShell

To remove all partitions on disks 1 and 2 without confirmation, run this command:

Get-Partition –DiskNumber 1,2 | Remove-Partition -Confirm:$false

Remove-Partition

To delete all partitions on Disk1 and completely erase data, use:

Clear-Disk -Number 1 -RemoveData -Confirm:$false

If there are OEM partitions on a disk (OEM recovery partition, EFI partition, System Reserved), use the RemoveOEM parameter to delete them:

Clear-Disk -Number 1 -RemoveData –RemoveOEM

Use the following PowerShell command to initialize any new RAW disks connected to the computer and create an NTFS partition with the maximum available size. It is convenient to use when connecting a new, empty disk (whether physical or virtual).

Get-Disk |Where-Object PartitionStyle -eq 'RAW' |Initialize-Disk -PartitionStyle MBR -PassThru |New-Partition -AssignDriveLetter -UseMaximumSize |Format-Volume -FileSystem NTFS -Confirm:$false

 This PowerShell one-liner is useful for quickly initializing a disk and creating partitions via the command line, particularly on Windows Server Core, which does not have a GUI.
7 comments
9
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows 11Windows Server 2025
previous post
Remote Desktop Cannot Verify the Identity of Remote Computer Because Time/Date Difference
next post
How to Create and Use a RAM Drive on Windows

Related Reading

Protecting Remote Desktop (RDP) Host from Brute Force...

February 5, 2024

How to Get My Public IP Address with...

October 24, 2023

Get-ADDomainController: Getting Domain Controllers Info via PowerShell

July 8, 2022

Backing Up Active Directory with Windows Server Backup

November 26, 2024

How to See Number of Active User Sessions...

March 16, 2024

Create & Manage DNS Zones and Records with...

April 3, 2023

Generating Strong Random Password with PowerShell

January 31, 2020

Match Windows Disks to VMWare VMDK Files

March 12, 2024

7 comments

ks February 3, 2020 - 11:46 am

thank you, this was very helpful

Reply
[email protected] March 1, 2020 - 12:55 am

Thanks so much, your post help me so much,

i did the follow script for shrink and create a new partition with free space:
Exmaples: Disk = 0, Driveletter=K

New-Partition -DiskNumber 0 -UseMaximumSize -DriveLetter k

maybe this can help other people. 🙂

Reply
KERR August 4, 2020 - 5:52 am

It would’ve been more helpful to include commands that show how to automatically select the correct eg unformatted disk. Instead, the article shows that we have to figure out which disk which defeats the purpose of automating the tasks.

Reply
Jaime Rosario September 20, 2020 - 11:52 pm

I have a script I use to backup data using robocopy. Because it stores critical/sensitive data, I prefer to hide the partition rather than allowing Windows to mount it automatically, and only use the script to unhide the partition, backup the date with robocopy, and hide the partition again. I use the HDD/SSD serial number in order to mount the drive and run the script.

You can get the serial number with:

Get-Disk | Select-Object Number,SerialNumber

Here’s part of the code I use that work both old and recent Powershell versions. The storage device serial number is represented as ’53R1ALNUMB3R’. The drive has two partitions, one small FAT16 and the “hidden” NTFS (partition 3). I run this with elevated privileges:

# To maintain compatibility with old Powershell versions
if ( $PSVersionTable.PSVersion.Major -le 4 )
{ Get-Disk | Where-Object SerialNumber -EQ 53R1ALNUMB3R | Get-Partition | Where-Object PartitionNumber -EQ 3 | Set-Partition -IsHidden $false }
else
# For Powershell v5 or later
{ Get-Disk -SerialNumber 53R1ALNUMB3R | Set-Partition -PartitionNumber 3 -IsHidden $false }

Reply
photobug November 27, 2020 - 11:59 pm

I recall there being some commands that can allow one to reset and recreate some sort of system log in the SRP that can grow to fill up the entire SRP free space and then interfere with system operations such as Windows Backup and Update. I used them a couple years ago when Backup broke as a result. I remember you go into disk management, add a drive letter to the SRP, and then do these commands. One of the commands let you see the existence and size of this mystery log. Once done, remove the drive letter, things work again. Anyone recall what the commands are?

Reply
malon January 23, 2024 - 10:13 am

Hi
Is there a powershell script that can be run automatically to resize the disk space?
Thx

Reply
admin January 26, 2024 - 3:21 pm

What does automatic resizing mean?
Suppose you have unallocated space on your hard drive.You will only be able to use it to increase the size of the partition to the left of it.

$driveLetter = "C"
Get-Partition -DriveLetter $driveLetter
$MaxSize = (Get-PartitionSupportedSize -DriveLetter $driveLetter).sizeMax
$MaxSizeGB = [math]::Round($MaxSize/1024/1024/1024,2)
Write-Output " the maximum size is $MaxSizeGB GB"
if ((get-partition -driveletter $driveLetter).size -eq $MaxSize) {
Write-Output "The drive $driveLetter is already at its maximum drive size"
exit
}
Resize-Partition -DriveLetter $driveLetter -Size $MaxSize

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • How to Detect Which User Installed or Removed a Program on Windows

    June 23, 2025
  • Encrypt Any Client-Server App Traffic on Windows with Stunnel

    June 12, 2025
  • Failed to Open the Group Policy Object on a Computer

    June 2, 2025
  • Remote Desktop Printing with RD Easy Print Redirection

    June 2, 2025
  • Disable the Lock Screen Widgets in Windows 11

    May 26, 2025
  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Managing Printers and Drivers on Windows with PowerShell
  • Protecting Remote Desktop (RDP) Host from Brute Force Attacks
  • How to Set a User Thumbnail Photo in Active Directory
  • Implementing Dynamic Groups in Active Directory with PowerShell
  • Match Windows Disks to VMWare VMDK Files
  • Using SSL/TLS Certificates for Remote Desktop (RDP)
  • How to Get My Public IP Address with PowerShell
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top