Backtrack:  
 
by lunarg on August 31st 2015, at 11:45

Powershell can also handle queries through WMI, allowing you retrieve all kinds of system information from local and remote systems running Windows. This also includes information about volumes, logical drives and shares.

For this to work on remote systems, you need to have Remote Management enabled. Starting from Server 2012, this is already enabled by default.

The commands use the Get-WmiObject cmdlet to retrieve the information. If no computer name is specified, the information will be retrieved from the system running the cmdlet. In order to connect to a remote system, run the cmdlet while specifying the computer name of the remote host with the -ComputerName parameter.

For example, the following command retrieves logical disk information from a remote computer called SERVER01:

Get-WmiObject Win32_LogicalDisk -ComputerName SERVER01

The cmdlets below are to retrieve information from the local system. To retrieve it remotely, adjust the argument of the -ComputerName parameter. For local systems, of course, you can omit the -ComputerName localhost parameter entirely.

Retrieve logical disk information

The following command will display all logical drives (i.e. volumes with a drive letter, including network drives), along with their

Get-WmiObject Win32_LogicalDisk -ComputerName localhost | FT Name,DriveType,Size,FreeSpace -Auto

Name DriveType         Size    FreeSpace
---- ---------         ----    ---------
C:           3 485266841600 414403145728
E:           5
G:           4 622768156672  70321635328
H:           4 622768156672  70321635328
K:           4 622768156672  70321635328
L:           4 622768156672  70321635328
S:           4 622768156672  70321635328
Y:           4 299589693440 153824153600

The DriveType column indicates the type of logical drive. Possible values are:

TypeDrive type
0Unknown
2Removable disk (USB/FD)
3Local disk
4Network share
5CD/DVD/BD drive
6RAM disk

Retrieve volume (and mountpoint) information

NTFS volumes can also mounted as mount points, in which case they don't show up as a logical disk. You can retrieve volume information from a system with the following PS cmdlet. This will also include all volumes mounted on a mount point and volumes that aren't mounted at all.

Get-WmiObject Win32_Volume -ComputerName localhost | Select Name,Label,Capacity,FreeSpace,FileSystem,BootVolume,SystemVolume | FT

Name              Label                    Capacity        FreeSpace FileSystem    BootVolume     SystemVolume
----              -----                    --------        --------- ----------    ----------     ------------
\\?\Volume{6b8... System Reserved         366997504        113979392 NTFS               False             True
L:\LDF\           MYDB LDF             214612045824      33016512512 NTFS               False            False
L:\BCK\           MYDB BCK             117974233088     117840863232 NTFS               False            False
L:\TMP\           MYDB TMP             160924954624     160145342464 NTFS               False            False
L:\               CLSQLMYDB              5333053440       5094289408 NTFS               False            False
L:\NDF\           MYDB NDF            2198886936576     222270517248 NTFS               False            False
L:\MDF\           MYDB MDF            2198886936576     756271796224 NTFS               False            False
C:\                                    299589693440     153832558592 NTFS                True            False

Retrieve share information

You can also retrieve information about shared folders:

Get-WmiObject Win32_Share -ComputerName localhost | Select Name,Path,Description | FT -Auto

Name   Path            Description
----   ----            -----------
ADMIN$ C:\Windows      Remote Admin
C$     C:\             Default share
D$     D:\             Default share
DSL    D:\DSL
F$     F:\             Default share
Ghost  F:\Ghost Images
IPC$                   Remote IPC

Combining information

The commands above can also be combined, allowing you to cross-reference shares with the associated volumes or logical drives.

Examples of these scripts can be found on this Technet blog (written by Jose Barreto).

For convenience (and in case the link goes down), I've added these scripts to this article as well. There are four scripts, two for each function and two other, which are their "one-liner" equivalents of the same script.

When running the scripts for a remote host, be sure to replace the argument for the -ComputerName (one-liner: -CN) parameter of the Get-WmiObject cmdlet to the computer name (or FQDN) of the remote host.