Friday, December 1, 2023

How to manage disks with PowerShell?

Date:

HomeHow ToHow to manage disks with PowerShell?

Related posts

Unlock the Power of Automation: Learn PowerShell in 2023 for Greater Productivity

Why you should learn PowerShell and how to get started with this powerful automation tool. Explore the key differences between CMD and PowerShell.

Linux Shell: Discover the 5 Inspiring Powers – A Beginner’s Guide

Explore the Fascinating World of Linux Shell and Architecture: A Beginner's Guide to Empower Your Linux Journey

We can manage our disks entirely using PowerShell. Most of you might have used GUI to delete a partition, initialize a disk, uninitialize a disk, create a partition, or format a partition. Let’s now see how we can perform the same tasks using PowerShell.

I have a virtual machine running Windows 10 Operating System. To this virtual machine, I have added a 10 GB disk. Now when I log into this machine and open the disk management console, it gives me a screen saying “You must initialize a disk before Logical Disk Manager can access it”. And in the disk list, it shows Disk 1 as shown in the picture below. This is the same 10 GB disk that we have added to this machine.

Obviously, we can click on OK here to initialize the disk, and then we can create a partition from this disk. But let’s see how we can do the same tasks using PowerShell.

List disks on the system with PowerShell

To get information about any disk attached to the current operating system, we can use the cmdlet Get-Disk. Run this cmdlet in PowerShell, and it will display the disks available on the machine. It shows us both any available partition on our machine or any other disks that have recently been added to the machine. This newly added disk will be in a raw partition style state as shown in the picture below.

This 10 GB RAW disk shown as disk 1 in the PowerShell output above is the same uninitialized disk that we saw when we opened the disk management GUI. We added a 10 GB LUN into our machine, and PowerShell is showing this uninitialized disk as RAW.

So we need to initialize this disk first in order to use it in our system.

Initialize a disk on PowerShell

To initialize a disk, we can use the cmdlet Initialize-Disk.

To this cmdlet, we can use the –Number parameter and specify the disk number that we want to initialize. So since we have to initialize Disk 1, we can write our command as:Initialize-Disk -Number 1

But if run this command, then the system will initialize our disk as GPT (GUID Partition table by default). If you want to initialize the disk with some other partition style, then you will need to use the parameter –PartitionStyle with the above command and then specify the partition style.

So for example, If I want to initialize Disk 1 with an MBR partition then I will write my command as: Initialize-Disk -Number 1 –PartitionStyle MBR

And if we run this command in PowerShell, then it will initialize our disk with an MBR partition. We can validate this by running Get-Disk the cmdlet once again, as shown in the picture below.

Uninitialize a disk on PowerShell

We have just seen how to initialize a RAW disk. There can also be a scenario where you would need to uninitialize a disk.

To uninitialize a disk, we can use the cmdlet: Clear-Disk.

Suppose I want to uninitialize Disk 1 once again. So for this, we can use the cmdlet: Clear-Disk and then specify the disk number in –Number parameter value:

Clear-Disk -Number 1

Let’s run this command and then validate it with Get-Disk cmdlet.

As you can see in the output above, when we ran our command to an uninitialized disk, it asked us for confirmation. It is an unrecoverable process, and PowerShell asks us to confirm if we want to proceed with the information. Just press the Enter button, and it will take the default value as ‘Y’. You can see in the output above that our Disk 1 has been uninitialized, and it has again become a RAW disk.

Now the Clear-Disk cmdlet here removed all partition information and uninitialize disk. But if your target disk is a data partition, then you must backup your data and then clear the target disk containing the data partition. For this, we just need to add the switch parameter –RemoveData. We have discussed about switch parameters in our previous post on PowerShell parameter types.

So suppose Disk 1 in one of my machines is a data partition, then I can clear the target disk and uninitialize it using the command: Clear-Disk -Number 1 –RemoveData

Create a Partition

To create a partition using PowerShell, we can use the cmdlet New-Partition. With this cmdlet, we can use the parameter ‑AssignDriveLetter to assign a letter to the partition drive and UseMaximumSize the parameter to assign the maximum size available on the disk.

Both ‑AssignDriveLetter and UseMaximumSize are switch parameters. This means the system will assign a drive letter randomly with the maximum available size. We also have the option to explicitly specify the disk size and drive letter. Let’s look into each of these options one by one.

I have a machine with two RAW disks (Disk 1 and Disk 2) of 10 GB each, as you can see in the picture below.

Now let’s create a partition in Disk 1 using switch parameters ‑AssignDriveLetter and UseMaximumSize.

For this, we will first initialize Disk 1 as we did earlier with the below commands:

  • Initialize-Disk -Number 1 -PartitionStyle MBR
  • Get-Disk

Now to create a new partition in Disk 1 we can write:

New-Partition -DiskNumber 1 -AssignDriveLetter -UseMaximumSize

And if we run this command, it will create a partition with the maximum available disk size and will also assign a drive letter as shown below.

So now we have two partitions on our computer. One is the C drive and another one is the G drive. We can get this information by running the Get-Partition cmdlet as shown below.

We still have one 10 GB RAW disk (Disk 2) available in this system, as you can see below.

Let’s go ahead and create another partition using Disk 2. But this time we will assign the drive letter ‘T’ ourselves, and we will make this partition of just 5 GB out of 10 GB available in Disk 2.

So for this, we will first initialize Disk 2. Then we will use the New-Partition cmdlet with the parameter –DriveLetter and –Size.

New-Partition -DiskNumber 2 -DriveLetter T -Size 5GB

Let’s go ahead and run this command.

So we have created a partition on both the RAW disks that were available to us. We can get the partition details of all of our disks by specifying disk numbers in our Get-Partition cmdlet as shown below.

Get-Partition -DiskNumber 0,1,2

Now our partition is created. But these partitions would still not be accessible from File Explorer. If I open File Explorer I get a message like the one shown below.

To access our partition from File Explorer, we have to create new volumes in a format (like NTFS) that the operating system understands. Let’s see how we can do that.

Create a Volume

We have already created partitions. Now, to create a volume, we need to format the partition in a format such as NTFS. But before we do that, let’s first try to list the volume on our computer. For this, we can use the cmdlet Get-Volume.

As you can see in the output above, it shows the operational status of drives G and T as unknown. This is going to change once we format these volumes.

To format the volumes, we can use the cmdlet Format-Volume. And to this cmdlet we can assign the Drive letters for the drives that we want to format using the cmdlet –DriveLetter. And then we can specify the file system such as NTFS with which we want to format these volumes.

Format-Volume -DriveLetter G,T -FileSystem NTFS

Let’s go ahead and run this command.

And let’s validate the volume status using Get-Volume cmdlet.

Get-Volume
Get-Volume

And you can see in the output above that the operational status of both the volumes (G and T) has changed to ‘OK’ status.

Delete a Partition

Now let’s see how we can delete these partitions that we have created. To delete the partition, we can use the cmdlet Remove-Partition. A simple way to use this cmdlet would be to first retrieve the partition details that we want to delete using the cmdlet Get-Partition and then pipe the output to the cmdlet Remove-Partition as shown below.

Get-Partition -DiskNumber 1,2 | Remove-Partition

Now if we run this command then it again asks us to confirm whether we want to delete the partition or not.

You can confirm to remove the partition. Or if you do not want to receive this message, then you can simply add the risk mitigation parameter –Confirm. We have already discussed about risk mitigation parameters in our article about common parameters in Powershell.

So if we use this risk mitigation parameter in our command and assign a value $False then it will not ask us for confirmation and the partition will simply be removed as shown below.

Now if you run Get-Volume cmdlet again you will find that the “G” and “T” drives have disappeared as shown below.

This is because we have removed the partition.

However, the two disks (Disk 1 and Disk 2) are still initialized and mapped to this virtual machine.

I will go ahead and uninitialize these disks using the command:

Clear-Disk 1,2 -Confirm:$False

Notice again we have used the risk mitigation parameter to suppress the confirmation message, Let’s go ahead and run this command.

And you can see that Disk 1 and Disk 2 have changed back to the RAW disk state. Now I can go ahead and unmap these disks from my virtual machine.

Manu
Manuhttps://toptechnotes.com
I'am IT Professional & Owner of toptechnotes.com I have been working in IT industry for more than 15 years. Apart from work, I love to read books and share knowledge.

Latest posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here