Essential PowerShell Cmdlets for Auditing and Maintaining Storage
For those of you who prefer to work at the command line, you'll love the power of PowerShell when working with storage. You're not limited to using local storage; you can work with network-attached storage, leveraged storage, ISO images, and virtual disks. With more than 100 storage related cmdlets at your disposal, you can experiment and explore your storage in a whole new way.
PowerShell 4.0 is the latest version of Microsoft's built-in Windows scripting language. It is the most complete and most powerful version to date. If you don't have version 4.0 on your system, visit Microsoft's Download Center to download and install the Windows Management Framework 4.0 package.
PowerShell 4.0 contains just over 1,300 cmdlets. And of those 1,300 cmdlets, 102 are storage related. You can use PowerShell storage cmdlets to view, create, edit, remove, mount, unmount, and repair physical disks, storage pools, and virtual disks. One point to remember about PowerShell is that you can use the "Get" cmdlets without fear of changing anything on your system. The "Get" cmdlets return information about your environment without executing any changes, so you may run them at will. Read: PowerShell Basics: Intro to Tools, Cmdlets & Modules
The system used for this article is a Windows Server 2012 R2 virtual machine running on VMware Workstation 10. Its hardware complement consists of 1GB RAM, 1 vCPU, one 50GB system disk, and several smaller disks for demonstration purposes.
Getting Storage Information
Your screen results might look different than those shown in this article because for demonstration purposes, all cmdlets are executed with an auto-formatted table option, although only the original cmdlet is shown. For example:
PS C:> Get-Disk | Format-Table -Auto
Will be shown as:
PS C:> Get-Disk
Also note that only the long forms of all cmdlets are used with formal capitalization.
The output from the Get-Disk cmdlet is:
Figure 1: Display of all recognized disks.
You see that PowerShell correctly identifies the disks as VMware virtual disks. However, the virtual machine’s operating system (Windows Server 2012 R2) recognizes the disks as physical and not as virtual, as shown in the following Get-PhysicalDisk cmdlet and results.
The Get-Disk cmdlet is an excellent first command to run on any system to which you connect. It lists all disks visible to the operating system and includes operational status, size, and partition style (formatting).
PS C:> Get-PhysicalDisk
Figure 2: A listing of all physical disks.
And the Get-VirtualDisk cmdlet, returns no results.
To view which disks have been formatted, use the Get-Volume cmdlet.
PS C:> Get-Volume
Figure 3: List all volume objects known to the system.
The results of the Get-Volume cmdlet displays all volumes on all partitions on all disks. You can specify drive letters to view select volumes. Separate drive letters with a comma and use the –DriveLetter option.
PS C:> Get-Volume –DriveLetter C,D
Figure 4: List only system volume objects by drive letters C and D.
Get-Volume returns errors if you specify drive letters that don’t exist on your system. Note that requesting volume information returns any mounted CD/DVD disks or ISO images. The VMware Tools image is an ISO disk image file, but the operating system displays it as if it were a physical CD disk in the CD disk drive.
The Get-Partition cmdlet returns information about any partitioned disks recognized by your operating system.
PS C:> Get-Partition
Figure 5: List disks that have configured partitions.
Partition 1 is a reserved partition that contains the boot files for your Windows installation. Partition 2 is the actual Windows partition and, as you can see, it is 50GB less the 350MB for the boot partition.
On this demonstration system, you have one visible disk and four disks that are currently offline. Looking again at the output from Get-Disk:
Figure 6: Listing of disks and their operational statuses.
Prepping Storage for Use
To bring a disk online, you first have to initialize it using the Initialize-Disk cmdlet.
PS C:> Initialize-Disk –Number 1
After a few seconds, or minutes depending on the size of the disk, the system returns your prompt. Check that the disk is ready for formatting with another Get-Disk.
PS C:> Get-Disk –Number 1
Figure 7: Disk 1 is now online.
Create a new partition on Disk 1.
PS C:> New-Partition -DiskNumber 1 -AssignDriveLetter -UseMaximumSize
Figure 8: Disk 1 partitioned and a drive letter (E) assigned.
Now that you've initialized and partitioned the disk, it's ready for formatting.
You can, at this point, issue a single cmdlet to format all partitioned disks:
PS C:> Format-Volume
Or, you can specify parameters for a single disk at a time, with a command similar to:
PS C:> Format-Volume –DriveLetter E –FileSystem NTFS
Figure 9: The formatting of Disk 1 (Drive E).
Your new disk (Drive E:) is ready to use.
Disk Optimization and Repair
You can issue optimize (Defragment) and repair cmdlets on your new volume.
PS C:> Optimize-Volume –DriveLetter E
You’ll likely see no response from the cmdlet as this process takes place. To repair a volume that you suspect has errors, has given you errors, or has recorded errors in the Event Log, issue the following cmdlet:
PS C:> Repair-Volume –DriveLetter E
This cmdlet takes the volume offline and performs a CHKDSK /F on the specified volume.
You can delete a partition with the Remove-Partition cmdlet.
PS C:> Remove-Partition –DriveLetter E
This action removes the disk from the available filesystems list and returns the drive letter to the available drive letter pool. It does not take the drive offline.
Labeling Disk Volumes
To label a disk, use the Set-Volume cmdlet.
PS C:> Set-Volume –DriveLetter E –NewFileSystemLabel "FiveGB"
Figure 10: Labeling the 5GB Drive as "FiveGB."
Shrinking Disk Partitions
You can easily resize a partition with the Resize-Partition cmdlet. First, you have to check the partition number on the target disk.
PS C:> Get-Partition -DiskNumber 1
Figure 11: Displaying Disk 1’s current size and configuration.
The following cmdlet sets the 5GB partition size on disk 1 to 2GB. Partition 2 is the target partition to resize.
PS C:> Resize-Partition -DiskNumber 1 -PartitionNumber 2 -Size (2GB)
PS C:> Get-Partition -DiskNumber 1
Figure 12: Displaying Disk 1's configuration after partition resize.
You resized Partition 2 to 2GB. The disk size has not changed and you can verify that with a Get-Disk cmdlet. You can add a new partition to that disk any size up to ~3GB.
PS C:> New-Partition -DiskNumber 1 -UseMaximumSize -AssignDriveLetter
Figure 13: Adding a new partition to Disk 1.
Check the new partition layout.
PS C:> Get-Partition -DiskNumber 1
Figure 14: Disk 1 now has two partitions of 2GB and 3GB.
To use the new 3GB partition (3) on disk 1, you’ll have to format it. Changing the partition table destroys the formatting for that partition.
PS C:> Format-Volume -DriveLetter F
Figure 15: Formatting partition 2 (Drive F:) on Disk 1.
You successfully formatted drive F: and it’s ready to use.
Enlarging Disk Partitions
Shrinking partitions via the Resize-Partition cmdlet is a useful capability and one that you’ll likely use often to arrange your partitions in order to maximize usage or to separate data. Fortunately, you can also enlarge partitions that you’ve set to a size that’s too small for your data or your applications.
In the following example, you have a disk that’s 2GB in size but you have a 5GB disk that you can use, so you want to extend that partition to 5GB. Actually, there’s a bit of overhead to formatting a disk or partition, so you won’t get the whole 5GB, but you’ll have approximately 4.99GB, after you extend the disk.
Extend the disk to its known maximum size with the following cmdlet:
PS C:> Resize-Partition -DiskNumber 1 -PartitionNumber 1 -Size (5GB)
Figure 16: Resize-Partition failure due to insufficient disk space.
You see the error that explains that the size you’ve requested isn’t supported. You can’t extend the partition to the 5GB point. You have to back off of the maximum due to filesystem formatting overhead. Begin with 4.99GB and work your way backwards in 0.01GB increments until your system returns a successful partition resize. This one works at 4.99GB.
PS C:UsersAdministrator> Resize-Partition -DiskNumber 1 -PartitionNumber 1 -Size (4.99GB)
PS C:UsersAdministrator> Get-Partition -DiskNumber 1
Figure 17: Drive E: successfully extended and ready for use.
You don't have to reformat the disk when you extend a formatted partition, but only when you shrink one as you saw in a previous example.
This article gives you a brief introduction to using PowerShell cmdlets to work with storage. Though not as intuitive as the graphical interface and Windows applications, PowerShell gives you the ability to quickly repeat tasks, to automate common operations, and to script solutions so that you can be more consistent and more productive in your environment.