Every Windows system administrator must deal with disks and storage (either physical or virtual) on a regular basis. While a few utilities (such as disk management and diskpart) are built into Windows that you can use to perform various disk management tasks, there are still some folks who love doing things with PowerShell. In this series of three posts, I will discuss all the PowerShell needed to manage disks.
In this article, I will try to cover most of the disk and storage management tasks that an administrator might need to perform—all with the help of Windows PowerShell.
Data loss warning
The commands in this series deal with various disk management activities, such as partitioning, formatting, converting, resizing, etc., which could cause permanent data loss. It is highly recommended that you back up your important data before running these commands. I will not be held responsible for any data loss incurred due to an incorrect command.
Disk partitioning schemes
The disk partitioning scheme defines how the boot structure and data are handled by the disk. When you attach a new disk in Windows and open the disk management utility, it asks you to initialize the disk with either the master boot record (MBR) or the globally unique identifier partition table (GPT) partition style. Let me briefly explain these partition schemes.
- MBR—The MBR is an old-fashioned partitioning scheme that has a limit of a maximum of four primary partitions and a maximum supported disk size of 2 TB.
- GPT—The GPT is a modern partitioning scheme that is more resilient and reliable and does not suffer from the limitations of the MBR scheme.
Limitations of MBR partitioning
- The MBR scheme allows the creation of a maximum of four primary partitions. If you want more, you need to create three primary partitions and one extended partition. The extended partition can be further partitioned into logical drives.
- With the MBR scheme, the maximum supported disk size is up to 2 TB. If you initialize a 4 TB drive as MBR, the disk will recognize the 2 TB space only, and the rest of the space will be wasted.
- The MBR scheme is supported by systems with legacy BIOS firmware.
- The MBR scheme is more prone to errors, corruption, and data loss.
Benefits of GPT partitioning
- As it is a modern specification, the GPT supports an almost unlimited number of partitions. However, the Windows implementation restricts this limit to 128 partitions.
- With the GPT scheme, the maximum supported GPT disk size can be up to 9.4 ZB (1 ZB = 1 billion TBs), which is way too much for current storage drives.
- The GPT scheme is supported by systems with Unified Extensible Firmware Interface (UEFI) firmware.
- The GPT scheme is more resilient to errors and corruption, which makes it more reliable than MBR.
- The GPT scheme provides a cyclic redundancy check (CRC) for data integrity.
Windows 10 and later now has native support for GPT disks, which means you can install Windows on a GPT disk, and it will work just fine as long as your system supports the UEFI firmware. Therefore, the GPT partition scheme is recommended for new disks, particularly if your disk is larger than 2 TB. The MBR scheme is only useful for compatibility with legacy systems that do not support UEFI firmware.
Finding suitable PowerShell disk management cmdlets
Before actually diving into the disk and storage management stuff, let me tell you a trick for finding a cmdlet in PowerShell that might be suitable for your needs. Remember that PowerShell cmdlets follow a "Verb-Noun" nomenclature. This means that almost every cmdlet has a "Verb" part and a "Noun" part separated by a "-" character (for example, Get-Service).
The "Verb" part of the cmdlet generally has fewer options, such as Get, Set, New, Remove, Start, Stop, Enable, Disable, Clear, etc. The Verb part in the cmdlet is usually self-descriptive.
Remember these rules of thumb:
- When you're trying to show or view something, you will use the cmdlets containing the "Get" verb.
- When you're trying to modify or update something, you'll use the cmdlets containing the "Set" verb.
- When you're trying to create or add something, you will use the cmdlets containing the "New" verb.
- When you're trying to delete or remove something, you will use the cmdlets containing the "Remove" or "Clear" verb.
Please be aware that these rules are not always strictly followed, but they work most of the time.
To figure out the "Noun" part of the cmdlet, you have to make an educated guess, depending upon the operation you will be performing. For example, we'll be dealing with storage and disks in this guide. So, most probably, the "Noun" part of our cmdlet should contain a "disk" keyword. Now, to get the information about a disk, the name of the cmdlet should look something like "Get-Disk." Do you get the idea of how the cmdlet names are chosen in PowerShell?
Furthermore, PowerShell has a Get-Command cmdlet that you can always use to figure out the cmdlet of your choice. It allows you to specify -Verb and -Noun parameters. We will use it in the next section. With the help the wildcard you can easily find the name of the right PowerShell disk management cmdlets.
Subscribe to 4sysops newsletter!
In the next post of my PowerShell disk management series, I will discuss the PowerShell storage module and its cmdlets for initializing, partitioning, formatting, and resizing disks.