Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

June 7, 2023 PowerShellWindows 10Windows Server 2016

Disks and Partitions Management with Windows PowerShell

In this article we’ll look on the disk, partition and volume management from PowerShell console. You can perform from PowerShell all the operations of managing local disks and partitions, that you are used to performing from the “Disk Management” GUI (diskmgmt.msc) or from the diskpart.exe command line tool. Disk management cmdlets are included in the Storage module available in PowerShell 3.0. We’ll consider how to initialize a disk, create a partition table on it, create a volume and format it. The commands given below will work in Windows 10 / Server 2016 and Windows 8.1 / Server 2012 R2 (for previous Windows versions you will have to update PowerShell first).

Contents:
  • PowerShell: List Local Disks and Partitions
  • Disk Initialization in PowerShell
  • How to Create Partitions on a Disk?
  • Formatting a partition with PowerShell
  • How to Remove Partitions from a Disk?

There are 160 PowerShell cmdlets in the Storage module in Windows 10. To display all available commands related to disk management, run the following command:

Get-Command -Module Storage

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

Important. Be very careful when managing disks and partitions from PowerShell in order not to accidentally delete or format a partition containing data.

PowerShell: List Local Disks and Partitions

First of all, try to display the list of local disks available in your system at the logical level. To do it, run this command:

Get-Disk | ft -AutoSize

To select only the system disk on which Windows is installed, enter the following command:

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

As you can see, the command has returned the following attributes of the system disk (you can use them in the selection as well):

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

You can display Offline disks only:

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

powershell Get-Disk offline and system

If you need the information about physical disks (the characteristics and status of physical disks on a computer), use Get-PhysicalDisk cmdlet (previously we showed how to detect a failed physical disk in Storage Spaces Direct using Get-PhysicalDisk cmdlet and how to use it then configure a fault tolerant S2D storage).

Get-PhysicalDisk info from powershell

You can detect the type of connected disk: SSD, HDD (usually connected over SATA bus) or 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

 

You can display the list of partitions on all disks:

Get-Partition

Or partitions on the specified disks only:

Get-Partition –DiskNumber 1,2

To display the list of all volumes in Windows, run this command:

Get-Volume

Get-Volume cmdlet

Please note that the disk numbering starts from 0, and partition numbering – from 1.

Disk Initialization in PowerShell

In the previous example you have seen that one of the disks is Offline and has a RAW label in the Partition Style column. Let’s try to initialize it, create a GPT or MBR partition table and create a new partition on it.

First of all, you must get the disk Online:

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

Now you can initialize it (its index is 1):

Initialize-Disk -Number 1

Get-Disk online and Initialize-Disk in powershell

By default, a GPT (GUID) partition table is created on a disk, but if you need an MBR one, run this command:

Initialize-Disk 1 –PartitionStyle MBR

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.

In order not to specify the disk number, you can initialize all disks with the RAW partition table:

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

Please note that a disk may have the RAW status when the partition table is corrupted. You can try to recover the partition table and data on your RAW disk using the testdisk tool.

How to Create Partitions on a Disk?

To create a new partition on a disk, the New-Partition cmdlet is used. 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 assign a letter automatically, the AssignDriveLetter parameter is used (sometimes Windows doesn’t assign a drive letter automatically).

New-Partition –DiskNumber 1 -AssignDriveLetter –UseMaximumSize

You can change the assigned letter using this command:

Set-Partition –DriveLetter L -NewDriveLetter U

If you want to expand the existing partition, first of all display the available unallocated space to extend this partition:

Get-PartitionSupportedSize -DriveLetter L | Format-List

Then you can extend the size of the partition to the maximum:

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

Resize-Partition from posh

If you want to make a partition active, this command is used:

Set-Partition -DriveLetter U -IsActive $true

Formatting a partition with PowerShell

Let’s format new partition in the NTFS and set the DBData volume label:

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

Format-Volume

How to Remove Partitions from a Disk?

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 from disks and completely clear data, run the command

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 remove them:

Clear-Disk -Number 1 -RemoveData –RemoveOEM

The next PowerShell one-liner will initialize all new connected RAW-type disks, create the partition table on them and create an NTFS partitions with the maximum available size. It is convenient to use it when connecting a new disk:

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

5 comments
5
Facebook Twitter Google + Pinterest
previous post
Remote Desktop Cannot Verify the Identity of Remote Computer Because Time/Date Difference
next post
How to Create a RAM Disk on Windows Server?

Related Reading

How to Use Ansible to Manage Windows Machines

September 25, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

Configure Email Forwarding for Mailbox on Exchange Server/Microsoft...

September 14, 2023

How to View and Change BIOS (UEFI) Settings...

September 13, 2023

How to Create UEFI Bootable USB Drive to...

September 11, 2023

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023
  • How to Create UEFI Bootable USB Drive to Install Windows

    September 11, 2023
  • Redirect HTTP to HTTPS in IIS (Windows Server)

    September 7, 2023
  • Add an Additional Domain Controller to an Existing AD Domain

    September 6, 2023
  • How to Install an SSL Certificate on IIS (Windows Server)

    September 5, 2023
  • Managing Windows Firewall Rules with PowerShell

    August 31, 2023
  • Fixing ‘The Network Path Was Not Found’ 0x80070035 Error Code on Windows

    August 30, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • RDP Brute Force Protection with PowerShell and Windows Firewall Rules
  • Active Directory Dynamic User Groups with PowerShell
  • Match Windows Disks to VMWare VMDK Files
  • Auditing Weak Passwords in Active Directory
Footer Logo

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


Back To Top