Disks, partitions and volumes have the following relation:
Let’s focus on every one of them on the following sections.
To list all operating system visible disks type:
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
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
To initialize a disk (disk numb 3) in order to create partitions and volume type:
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
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
A Disk object can have one or more logical regions called partitions.
To list all partition on all disks, type:
To list all partition of a specific disk (disk numb 3), type:
Get-Partition –DiskNumber 3
To create a new partition (on disk numb 3), type:
New-Partition –DiskNumber 3 –UseMaximumSize -AssignDriveLetter
To list all volumes of a drive letter (drive letter C), type:
Get-Volume –DriveLetter C
To format a volume (drive letter C) with NTFS, type:
Format-Volume –DriveLetter C
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
Note: Get-PSDrive alias is gdr
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
To see storage setting like disk policy (previously SAN policy), type:
To change a storage setting like new disk policy, type:
Set-StorageSetting –NewDiskPolicy OfflineShared
These are the existing disk policy options:
|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 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:
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