Skip to content

Instantly share code, notes, and snippets.

@trackd
Last active July 19, 2024 11:04
Show Gist options
  • Save trackd/51dcb587452eb9dc0fc3c46641e1c2dc to your computer and use it in GitHub Desktop.
Save trackd/51dcb587452eb9dc0fc3c46641e1c2dc to your computer and use it in GitHub Desktop.
function Get-MyDisk {
[CmdletBinding()]
param(
[Alias('DeviceId')]
[int] $Number,
[string] $FriendlyName
)
if (-Not ('Pinvoke.Win32Utils' -as [type])) {
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
using System.Text;
namespace pinvoke {
public static class Win32Utils {
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
}
}
'@
}
$sbDeviceName = [System.Text.StringBuilder]::new(1024)
foreach ($Disk in (Get-Disk @PSBoundParameters)) {
foreach ($Partition in (Get-Partition $Disk.Number)) {
foreach ($Volume in (Get-Volume -Partition $Partition)) {
if ($Volume.DriveLetter) {
[void][PInvoke.Win32Utils]::QueryDosDevice(($Volume.DriveLetter + ':'), $sbDeviceName, $sbDeviceName.Capacity)
}
[PSCustomObject]@{
PSTypeName = 'Custom.MyDisk'
Disk = $Disk.Number
Health = $Disk.HealthStatus
FilesystemLabel = $Volume.FilesystemLabel
DiskSizeGB = [Math]::Round(($Volume.Size / 1GB), 2)
DiskfreeGB = [Math]::Round(($Volume.SizeRemaining / 1GB), 2)
Partition = $Partition.PartitionNumber
DriveLetter = $Volume.DriveLetter
FriendlyName = $Disk.FriendlyName
Harddiskvolume = $sbDeviceName.ToString()
# Bootdisk = $disk.BootFromDisk
# IsBoot = $partition.IsBoot
# SystemPartition = $partition.IsSystem
}
[void]$sbDeviceName.Clear()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment