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://mcmap.net/q/715596/-combine-get-disk-info-and-logicaldisk-info-in-powershell
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
$info_diskdrive_basic | Format-List
instead of doing those steps 1, 2 and 3 ? – MargueritamargueriteDescription: Value
so I want to go with the same for this. – WardshipFormat-List
produces, but with a lot of extra whitespace between the property name and the actual value? Again: WHY? – Margueritamarguerite