Search code examples
powershellget-wmiobject

Combine `Get-Disk` info and `LogicalDisk` info in PowerShell (but with formatted output)


This is a question about an answer for this Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?

Here is the answer which I've tried to change to get the output formatted how I want it: https://stackoverflow.com/a/31092004/8262102

It needs to work for multiple drives like the code below only in the desired format.

This is the code with all the details on what my attempts are to do so:

$info_diskdrive_basic = Get-WmiObject Win32_DiskDrive | ForEach-Object {
  $disk = $_
  $partitions = "ASSOCIATORS OF " + "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " + "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
  Get-WmiObject -Query $partitions | ForEach-Object {
    $partition = $_
    $drives = "ASSOCIATORS OF " + "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " + "WHERE AssocClass = Win32_LogicalDiskToPartition"
    Get-WmiObject -Query $drives | ForEach-Object {
      [PSCustomObject][Ordered]@{
        Disk          = $disk.DeviceID
        DiskModel     = $disk.Model
        Partition     = $partition.Name
        RawSize       = '{0:d} GB' -f [int]($partition.Size/1GB)
        DriveLetter   = $_.DeviceID
        VolumeName    = $_.VolumeName
        Size          = '{0:d} GB' -f [int]($_.Size/1GB)
        FreeSpace     = '{0:d} GB' -f [int]($_.FreeSpace/1GB)
      }
    }
  }
}

# Here's my attempt at formatting the output of the code above.

# 1. This trims the dead whitespace from the output.
$info_diskdrive_basic = ($info_diskdrive_basic | Out-String) -replace '^\s+|\s+$', ('')

# 2. I then separate the DiskModel, RawSize, DriveLetter, VolumeName, FreeSpace with the regexp below so this becomes:
# Disk Model, Raw Size, Drive Letter, Volume Name, Free Space
$info_diskdrive_basic = ($info_diskdrive_basic) -replace '(?-i)(?=\B[A-Z][a-z])', (' ')

# 3. Here I then format the string to how I want:
$info_diskdrive_basic = ($info_diskdrive_basic) -replace '(.+?)(\s+):\s*(?!\S)', ($id2 + '$1:$2                                       ')

$info_diskdrive_basic

The output should look like this:

I want to format the properties and the values like so: Properties: >spaces< value where the value is over to the right and aligned along the left of them

# Disk:                                                 \\.\PHYSICALDRIVE0
# Disk Model:                                           Crucial_CT512MX100SSD1
# Partition:                                            Disk #0, Partition #2
# Raw Size:                                             476 GB
# Drive Letter:                                         C:
# Volume Name:
# Size:                                                 476 GB
# Free Space:                                           306 GB

But my output ends up like this: (Notice how the text is not aligned)

# Disk:                                                \\.\PHYSICALDRIVE0
# Disk Model:                                           Crucial_CT512MX100SSD1
# Partition:                                           Disk #0, Partition #2
# Raw Size:                                             476 GB
# Drive Letter:                                         C:
# Volume Name:
# Size:                                                476 GB
# Free Space:                                           306 GB

Solution

  • To output the info as you apparently need, we need to know the maximum line length (which in your example is 79 characters) and work our way from that.

    $maxLineLength  = 79  # counted from the longest line in your example
    $maxValueLength = 0   # a counter to keep track of the largest value length in characters
    
    $info_diskdrive_basic = Get-WmiObject Win32_DiskDrive | ForEach-Object {
        $disk = $_
        $partitions = "ASSOCIATORS OF " + "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " + "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
        Get-WmiObject -Query $partitions | ForEach-Object {
            $partition = $_
            $drives = "ASSOCIATORS OF " + "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " + "WHERE AssocClass = Win32_LogicalDiskToPartition"
            Get-WmiObject -Query $drives | ForEach-Object {
                $obj = [PSCustomObject]@{
                    'Disk'         = $disk.DeviceID
                    'Disk Model'   = $disk.Model
                    'Partition'    = $partition.Name
                    'Raw Size'     = '{0:d} GB' -f [int]($partition.Size/1GB)
                    'Drive Letter' = $_.DeviceID
                    'Volume Name'  = $_.VolumeName
                    'Size'         = '{0:d} GB' -f [int]($_.Size/1GB)
                    'Free Space'   = '{0:d} GB' -f [int]($_.FreeSpace/1GB)
                }
                # get the maximum length for all values
                $len = ($obj.PsObject.Properties.Value.ToString().Trim() | Measure-Object -Property Length -Maximum).Maximum
                $maxValueLength = [Math]::Max($maxValueLength, $len)
                                              
                # output the object to be collected in $info_diskdrive_basic
                $obj
            }
        }
    }
    
    # sort the returned array of objects on the DriveLetter property and loop through
    $result = $info_diskdrive_basic | Sort-Object DriveLetter | ForEach-Object {
        # loop through all the properties and calculate the padding needed for the output
        $_.PsObject.Properties | ForEach-Object {
            $label   = '# {0}:' -f $_.Name.Trim()
            $padding = $maxLineLength - $maxValueLength - $label.Length
            # output a formatted line
            "{0}{1,-$padding}{2}" -f $label, '', $_.Value.ToString().Trim()
        }
        # add a separator line between the disks
        ''
    }
    
    # output the result on screen
    $result
    
    # write to disk
    $result | Set-Content -Path 'X:\theResult.txt'
    
    # format for HTML mail:
    '<pre>{0}</pre>' -f ($result -join '<br>')
    

    Example output:

    # Disk:                                              \\.\PHYSICALDRIVE1
    # Disk Model:                                        Samsung SSD 750 EVO 250GB
    # Partition:                                         Disk #1, Partition #0
    # Raw Size:                                          232 GB
    # Drive Letter:                                      C:
    # Volume Name:                                       System
    # Size:                                              232 GB
    # Free Space:                                        160 GB
    
    # Disk:                                              \\.\PHYSICALDRIVE2
    # Disk Model:                                        WDC WD7501AALS-00J7B0
    # Partition:                                         Disk #2, Partition #0
    # Raw Size:                                          699 GB
    # Drive Letter:                                      D:
    # Volume Name:                                       Data
    # Size:                                              699 GB
    # Free Space:                                        385 GB
    

    P.S. with creating [PsCustomObject], there is no need to add [Ordered]