Managing Storage with Windows PowerShell on Windows Server 2012
Managing local Windows Storage
This post covers basic provisioning operations against local Windows storage resources using the Storage module for Windows PowerShell.
Note: I will cover using the Storage module in combination with a Storage Management Provider to manage a storage subsystem (array) in a subsequent post.
The Disk, Partition, and Volume objects are associated as shown in the figure below. When starting from a new (blank) disk, you first initialize the disk to create a partition, and then you create a Volume with a file system such as NTFS.
The following sections discuss the operations that you can perform with each type of object – Disk, Partition, and Volume. They also discuss setting the policy of whether to automatically mount new disks, and how to use the various cmdlets to format a new disk for use.
Disk
A Disk object represents a disk as seen by a Windows computer, and does not require an available management provider. An example of a Disk object is a disk displayed in the Disk Management snap-in.
Listing all disks in the system
To list all Disks visible to the operating system, type:
Get-Disk
Listing only the system disk or all disks except the system disk
To list only the system disk, type the following command:
Get-Disk | Where-Object IsSystem -eq $True
To list all disks except the system disk, instead type this command, which pipes the output of the Get-Disk cmdlet to the Where-Object cmdlet:
Get-Disk | where-object IsSystem -eq $False
Listing all disks for a specific storage bus
To list all disks attached via a specific storage bus, type the following command, replacing <BusType> with the bus you want to query:
Get-Disk | Where-Object -Eq <BusType>
For example, to list all of the Storage Spaces that are connected, query disks connected via the “Spaces” bus. This method can also be used for other bus types, such as iSCSI and USB:
Clearing all of the partitions and volumes from a disk
To clear all partitions and volumes from a disk, backup all data on all volumes on the disk, and then type the following command:
Warning: This is operation cannot be undone; it will clear all partitions and volumes from the disk and allows the disk to be re-initialized and create a new partition and volume.
Clear-Disk 6 -RemoveData
Note: If the specified disk contains an OEM partition, such as for system recovery, it is also necessary to specify the –RemoveOEM switch when using the clear-disk cmdlet.
Initializing a disk
To initialize a disk to allow creation of a partition and volume, type the following command, where <DiskNumber> is the number of the disk to initialize:
Initialize-Disk <DiskNumber>
Note: All disks are initialized as GPT by default unless otherwise specified.
To initialize a disk as MBR, use the –PartitionStyle parameter. For example:
Initialize-Disk 4 –PartitionStyle MBR
Listing all disks that are offline
To list all disks that are currently offline, type the following command:
Get-Disk | Where-Object IsOffline –Eq $True
Bringing all offline disks online
To bring all disks that are currently offline back online, type the following command:
Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline $False
Partition
A Disk object can contain one or more logical regions called partitions, which are represented by a Partition object. With the exception of special purpose partitions such as a Microsoft Reserved (MSR), or OEM recovery partition, Microsoft recommends creating one data partition per disk.
Listing all of the partitions on all disks
To list all partitions on all disks, type the following command:
Get-Partition
Listing all of the partitions on a specific disk
To list all partitions on a specific disk, type the following command, replacing <DiskNumber> with the number of the appropriate disk:
Get-Partition –DiskNumber <DiskNumber>
Creating a new partition on a new disk
To create a new partition on a blank disk, first use the Initialize-Disk cmdlet, then type the following command, replacing <DiskNumber> with the number of the appropriate disk:
New-Partition –DiskNumber <DiskNumber> -UseMaximumSize -AssignDriveLetter
Volume
A Volume object is the highest level object in the Disk-Partition-Volume hierarchy and represents a Partition object that has been formatted with a file system such as NTFS or ReFS and is available for data storage.
Listing all volumes
To list all volumes accessible to Windows, type:
Get-Volume
Listing the volume for a specific drive letter
To list the volume for a specific drive letter, type the following, replacing <DriveLetter> with the letter of the drive you want to view:
Get-Volume –DriveLetter <DriveLetter>
Formatting a volume
To format a volume with the NTFS file system, type the following, replacing <DriveLetter> with the letter of the drive you want to format:
Format-Volume -DriveLetter <DriveLetter>
Storage Settings object
The new disk policy (previously known as the SAN Policy) is the policy Windows uses to determine whether Windows should automatically ‘mount’ disks that are detected as new.
Viewing the new disk policy
To view the new disk policy, type the following command:
Get-StorageSetting | Select-Object NewDiskPolicy
Set the NewDiskPolicy
To set the new disk policy, type the following command:
Set-StorageSetting –NewDiskPolicy OfflineShared
Note: The default setting for NewDiskPolicy on Windows 8 is OnlineAll. The default setting on all Windows Server 2012 editions is OfflineShared.
Policy Setting |
Effect |
OfflineAll |
All new disks are left offline by default. |
OfflineInternal |
All disks on busses that are detected as internal are left offline as default. |
OfflineShared |
All Disks on sharable busses, such as iSCSI, FC, or SAS are left offline by default |
OnlineAll |
All disks are automatically brought online. |
Combined Example: Format a New Disk
To format a new disk that has not been initialized, first we should get all Disk objects and then pipe the objects to the Where-Object cmdlet to select only disks with the RAW partition style (which indicates that the disks haven’t yet been initialized). To do so, type the following command:
Get-Disk | Where-Object PartitionStyle –Eq "RAW"
Then, use the Initialize-Disk cmdlet to initialize the disk. After that, use the New-Partition cmdlet to create a partition on the disk, and pipe the output to the Format-Volume cmdlet to format the volume with the NTFS file system. To do so, type the following commands:
Initialize-disk 3
New-Partition -DiskNumber 3 -UseMaximumSize -AssignDriveLetter | Format-Volume -NewFileSystemLabel "Mirror" -FileSystem NTFS
Bruce Langworthy
Senior Program Manager – Windows Storage and Filesystems