Powershell – Working with devices, partitions and volumes


Windows storage

Disks, partitions and volumes have the following relation:

Let’s focus on every one of them on the following sections.

Disk
Get-Disk
To list all operating system visible disks type:

Get-Disk

You’ll get something like:

Note: If you run Get-Disk without Admin rights you will receive the following error: “Access to a CIM resource was not available to the client”.
If you’re already Admin remember to run powershell console as Administrator or you will receive the same error.
To restrict query results we can pipe the output of Get-Disk to the Where-Object cmdlet.
To get all USB disks you have to type:

Get-Disk | Where-Object –FilterScript [$_.Bustype –Eq “USB”}

Note: ? is alias for Where-Object. So you can type the same command this hay:

Get-Disk | ? –FilterScript [$_.Bustype –Eq “USB”}

To list all system disks type:

Get-Disk | ? IsSystem –eq $True

Clear-Disk
To clear all partition and volumes from a disk (disk numb 3), type (you will lose all data and the operation can’t be undone):

Clear-Disk 3 -RemoveData

Note: If the disk has a OEM partition in it it’s also necessary to specify the –RemoveOEM parameter

Initialize-Disk
To initialize a disk (disk numb 3) in order to create partitions and volume type:

Initialize-Disk 3

Note: All disks are initialized as GPT (GUID Partition Table) unless you specify differently. To initialize as MBR (Master Boot Record), type:

Initialize-Disk 3 –PartitionStyle MBR

Set-Disk
To change disk properties you have to use Set-Disk.
In this case piping the result of the Get-Disk cmdlet and changing all objects (disks) to an online state. Just type:

Get-Disk | ? IsOffline | Set-Disk –IsOffline:$False

To change read-only disk property type:

Get-Disk | ? IsReadOnly | Set-Disk –IsReadOnly:$False

Partition
Get-Partition
A Disk object can have one or more logical regions called partitions.
To list all partition on all disks, type:

Get-Partition

To list all partition of a specific disk (disk numb 3), type:

Get-Partition –DiskNumber 3

New-Partition
To create a new partition (on disk numb 3), type:

New-Partition –DiskNumber 3 –UseMaximumSize -AssignDriveLetter

Volume
Get-Volume
To list all volumes of a drive letter (drive letter C), type:

Get-Volume –DriveLetter C

Format-Volume
To format a volume (drive letter C) with NTFS, type:

Format-Volume –DriveLetter C

Drives
Get-PSDrive
The following cmdlet gives to the current session the following drives:
– Windows logical drives, including mapped drives and network shares
– Drives exposed by powershell providers, HKLM, HKCU, Registry drives…
– Drives created with New-PSDrive

Get-PSDrive

Note: Get-PSDrive alias is gdr

New-PSDrive
Cmdlet used to map a drive to a path.
In this example we create a drive X mapped to folder C:\Scripts:

New-PSDrive -name X -psprovider FileSystem -root c:\scripts

Note: Get-PSDrive alias is ndr or you can use mount

Storage Settings
Get-StorageSetting
To see storage setting like disk policy (previously SAN policy), type:

Get-StorageSetting

Set-StorageSetting
To change a storage setting like new disk policy, type:

Set-StorageSetting –NewDiskPolicy OfflineShared

These are the existing disk policy options:

Policy settings Description
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

… WMI
WMI remains in most cases the primary automation technology for system administration, so Get-WmiObject cmdlet is essential in order to do routine management tasks. And unlike most cmdlets, Get-WmiObject can be run against remote computers.
To obtain information about volumes, type:

Get-WmiObject win32_logicaldisk

To obtain same information remotely but only for drive type 3, type:

Get-WmiObject –query “select * from win32_logicaldisk where drivetype=’3’” –computername “MYSERVERNAME”

The same output data on table format:

gwmi diskpartition –filter “drivetype=’3’” –computername “MYSERVERNAME” | format-table

…most commonly used
To physical disks information use class: win32_logicaldisk class
To see disk partitions information use: win32_diskpartition
To see map of physical disk and partition use class: win32_logicadisktopartition
To see volumes use: win32_volume
To check drives mapped to a computer use: win32_mappedlogicaldisk


In Depth
http://technet.microsoft.com/en-us/library/hh848705.aspx
http://blogs.msdn.com/b/san/archive/2012/07/03/managing-storage-with-windows-powershell-on-windows-server-2012.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2013/08/29/powertip-list-physical-drives-with-powershell.aspx
http://technet.microsoft.com/en-us/library/ee176915.aspx

Tested With
PowerShell 4.0

Advertisements
Categories: Powershell, WMI | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: