Weekend Scripter: Messing Around with Disks

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows 8 storage module to create partitions and to format disks. Microsoft Scripting Guy, Ed Wilson, is here. Today, I thought I would spend a bit more time playing around with the Windows 8 and Windows Server 2012 cmdlets in the storage module. To find these cmdlets, use the Get-Command cmdlet (gcm is the alias) and specify the storage module as shown here.

Gcm –module storage On my laptop there are 84 cmdlets—this is shown here.

15:44 C:> gcm -Module storage | measure

Count    : 84

Average  :

Sum      :

Maximum  :

Minimum  :

Property : Interestingly enough, 84 is nearly the number of cmdlets that shipped with Windows PowerShell 1.0—so there is an awful lot of stuff to be done with these functions.

CAUTION   Be extremely careful using the functions from the storage module. They all require Administrator rights (even to simply see disk information), and therefore, when running functions such as New-Partition or Format-Volume you already have the rights required to perform the action. If you are not very, very, very careful using these cmdlets, you can totally hose your computer to the point you will be doing a baremetal restore. For this reason, make sure you are extremely familiar with concepts such as disks, volumes, and partitions before trying any of these commands.

Use Windows PowerShell to work with partitions

Partitions are associated with disks, so the first thing I need to do is to find out information about the disks installed in my computer. To do this, I use the Get-Disk function. When used without any parameters, the Get-Disk function returns all of the disks associated with my computer. This command and associated output is shown here.

15:56 C:> get-disk

Number Friendly Name                 OperationalStatus  Total Size PartitionStyle

       —— ————-                            ————  ———- ———–

0      INTEL SSDSA2BW160G3L                     Online         149.05 GB MBR

1      Kingston DataTraveler 2.0 USB Device     Online      3.75 GB MBR From the above output, I see there are two disks. One is numbered disk number 0 and it is an Intel SSD type of drive that is 149 gigabytes in size. The other drive is drive number 1, and it is a Kingston USB type of device. Disk 1 is 3.75 gigabytes in size. Using this information, I now use the Get-Partition function to see how many partitions I have on disk 1. This command and associated output is shown here.

15:59 C:> Get-Partition -DiskNumber 1

     Disk Number: 1

 PartitionNumber  DriveLetter Offset       Size Type

—————  ———– ——               —- —-

1                E           65536                     3.75 GB FAT32

Note   Disks begin numbering at 0; partitions begin numbering at 1. To connect to a specific partition and to receive information about it, use the Get-Partition function and specify the partition number. This technique is shown here.

15:59 C:> Get-Partition -DiskNumber 1 -PartitionNumber 1

   Disk Number: 1

PartitionNumber  DriveLetter Offset            Size Type

—————  ———– ——                   —- —-

1                E           65536                     3.75 GB FAT32

Remove a partition

To remove (delete) a partition from a specific disk, use the Remove-Partition function. When using this function, I need to specify the disk number and the partition number. Luckily, I already know this from my earlier explorations by using Get-Disk and Get-Partition. By default, the function prompts for confirmation. This is shown here.

16:48 C:> Remove-Partition -DiskNumber 1 -PartitionNumber 1

Confirm

Are you sure you want to perform this action?

This will erase all data on disk 1 partition 1.

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help

(default is “Y”):y

16:49 C:> It is possible to suppress this confirmation. To do this, use the –confirm parameter and provide a value of $false. This command is shown here.

Remove-Partition -DiskNumber 1 -PartitionNumber 1 -Confirm:$false

Create a partition

To create a partition, use the New-Partition function. The minimum parameters required are the disk number and the size of the partition. If I do not know the size to create, I can use the –UseMaximumSize switched parameter. This is shown here.

16:52 C:> New-Partition -DiskNumber 1 -UseMaximumSize

    Disk Number: 1

 PartitionNumber  DriveLetter Offset                Size Type

—————  ———– ——                       —- —-

1                E           65536                     3.75 GB Logical I can also specify the size of the partition to create. I can use one of the following unit values: bytes, KB, MB, GB, or TB. If I do not specify a unit value, the function defaults to bytes. The following command creates a new partition on disk 1 with a size of 1 gigabyte.

17:11 C:> New-Partition -DiskNumber 1 -Size 1GB

   Disk Number: 1

PartitionNumber  DriveLetter Offset                Size Type

—————  ———– ——                       —- —-

1                E           65536                        1 GB Logical

Format the volume

Now I need to format the volume created when I created the partition. To do this, I can use the drive letter, or if I do not want to do that, I can use Windows PowerShell to find the information for me. To do this, I begin by using Get-Disk to return a disk object for disk number 1. I confirmed earlier, that disk 1 is the disk with which I want to work. Now, I pass this disk object to the Get-Partition function and return all the partitions from that disk. I know that there is only one partition, because I only created a single partition. I next pass that partition to the Get-Volume so I obtain a volume object. This I pass to the Format-Volume function, and I specify the file system type and the volume label. Available file system types are: NTFS, ReFS, exFAT, FAT32, and FAT.

get-disk -Number 1 | Get-Partition | Get-Volume | Format-Volume -FileSystem fat32 NewFileSystemLabel “newLabel” Because the New-Partition function returns a partition object, I can create the partition and format the volume all at the same time. This technique is shown here.

17:37 C:> New-Partition -DiskNumber 1 -Size 1GB | Get-Volume | Format-Volume -FileSy

stem fat32

Confirm

Are you sure you want to perform this action?

Warning, all data on the volume will be lost!

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help

(default is “Y”):y

DriveLetter  FileSystemLabel FileSystem  DriveType   HealthStatus SizeRemaining        Size

———–  ———– ———-  ———   ———– ———–        —-

E                        FAT32       Removable   Healthy         1020 MB     1020 MB If I want to create the partition and to format the partition without the prompt, I only need to specify the –confirm parameter and supply $false. This technique is shown here.

17:40 C:> New-Partition -DiskNumber 1 -Size 1GB | Get-Volume | Format-Volume -FileSystem fat32 -Confirm:$false

DriveLetter  FileSystemLabel FileSystem  DriveType   HealthStatus SizeRemaining        Size

———–  ———– ———-  ———   ———– ———–        —-

E                        FAT32       Removable   Healthy         1020 MB     1020 MB I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon