### Copyright (c) 2014, Svendsen Tech ### Author: Joakim Svendsen ### Get-MountPointData v1.1 # "Change history": 1.0 -> 1.1 = -Credential, -PromptForCredentials and -NoFormat (the latter allows math operations) # Convert from one device ID format to another. function Get-DeviceIDFromMP { param([Parameter(Mandatory=$true)][string] $VolumeString, [Parameter(Mandatory=$true)][string] $Directory) if ($VolumeString -imatch '^\s*Win32_Volume\.DeviceID="([^"]+)"\s*$') { # Return it in the wanted format. $Matches[1] -replace '\\{2}', '\' } else { # Return a presumably unique hashtable key if there's no match. "Unknown device ID for " + $Directory } } function Get-MountPointData { param( [Parameter(Mandatory=$true)][string[]] $ComputerName, [switch] $PromptForCredentials, [System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty, [switch] $IncludeRootDrives, [switch] $NoFormat ) foreach ($Computer in $ComputerName) { $WmiHash = @{ ComputerName = $Computer ErrorAction = 'Stop' } if ($PromptForCredentials -and $Credential.Username) { Write-Warning "You specified both -PromptForCredentials and -Credential. Prompting overrides." } if ($PromptForCredentials) { $WmiHash.Credential = Get-Credential } elseif ($Credential) { $WmiHash.Credential = $Credential } try { # Collect mount point device IDs and populate a hashtable with IDs as keys $MountPointData = @{} Get-WmiObject @WmiHash -Class Win32_MountPoint | Where { if ($IncludeRootDrives) { $true } else { $_.Directory -NotMatch '^\s*Win32_Directory\.Name="[a-z]:\\{2}"\s*$' } } | ForEach-Object { $MountPointData.(Get-DeviceIDFromMP $_.Volume $_.Directory) = $_.Directory } $Volumes = Get-WmiObject @WmiHash -Class Win32_Volume | Where { if ($IncludeRootDrives) { $true } else { -not $_.DriveLetter } } | Select-Object Label, Caption, Capacity, FreeSpace, FileSystem, DeviceID, @{n='Computer';e={$Computer}} } catch { Write-Error "${Computer}: Terminating WMI error (skipping): $_" continue } if (-not $Volumes) { Write-Error "${Computer}: No mount points found. Skipping." continue } $Volumes | ForEach-Object { if ($MountPointData.ContainsKey($_.DeviceID)) { if ($_.Capacity) { $PercentFree = $_.FreeSpace*100/$_.Capacity } else { $PercentFree = 0 } $_ | Select-Object Computer, Label, Caption, FileSystem, @{n='Size (GB)';e={$_.Capacity/1GB}}, @{n='Free space';e={$_.FreeSpace/1GB}}, @{n='Percent free';e={$PercentFree}} } } | Sort-Object -Property 'Percent free', @{Descending=$true;e={$_.'Size (GB)'}}, Label, Caption | Select-Object -Property $(if ($NoFormat) { @{n='ComputerName'; e={$_.Computer}}, @{n='Label'; e={$_.Label}}, @{n='Caption'; e={$_.Caption}}, @{n='FileSystem'; e={$_.FileSystem}}, @{n='Size (GB)'; e={$_.'Size (GB)'}}, @{n='Free space'; e={$_.'Free space'}}, @{n='Percent free'; e={$_.'Percent free'}} } else { @{n='ComputerName'; e={$_.Computer}}, @{n='Label'; e={$_.Label}}, @{n='Caption'; e={$_.Caption}}, @{n='FileSystem'; e={$_.FileSystem}}, @{n='Size (GB)'; e={$_.'Size (GB)'.ToString('N')}}, @{n='Free space'; e={$_.'Free space'.ToString('N')}}, @{n='Percent free'; e={$_.'Percent free'.ToString('N')}} }) } }