Home > Net >  Not able to select specific column in Get-Disk
Not able to select specific column in Get-Disk

Time:10-02

I have the below data in csv file

"Server","Disk ###","Status","Size","Free"
"s1","Disk 0","Online","100 GB","1024 KB"
"s1","Disk 1","Online","6144 MB","1024 KB"
"s1","Disk 2","Online","200 GB","1024 KB"
"s1","Disk 3","Online","10 GB","1024 KB"

expected:

"Server","Disk ###","Status","Size","Free", "Total Size"
"s1","Disk 0","Online","100 GB","1024 KB","100 GB"
"s1","Disk 1","Online","6144 MB","1024 KB","60 GB"
"s1","Disk 2","Online","200 GB","1024 KB","100 GB"
"s1","Disk 3","Online","10 GB","1024 KB","2.44 TB"

I want to add Total Size column to this from Get-Disk command

but I am not able to select the column

I tried using like below

Get-Disk | Select-Object Total Size
Get-Disk | Select-Object 'Total Size'

But it is giving blank output.

Please let me know what is the issue

tried like below

$dataa= Get-Disk | Select-Object -Property @{n="TotalSize";e={'{0:n2} GB' -f ($_.Size / 1GB)}} | ft

Import-Csv -path c:\infile.csv |
Select-Object *,@{Name='TotalSize';Expression={$dataa}} | 
Select-Object "Server","Disk ###","Status","Size","Free", "TotalSize" |
Export-Csv -path C:\Outfile.csv -NoTypeInformation

Getting data like

"Server","Disk ###","Status","Size","Free","TotalSize"
"s1","Disk 0","Online","100 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 1","Online","6144 MB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 2","Online","200 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
"s1","Disk 3","Online","10 GB","1024 KB","Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"

CodePudding user response:

When Get-Disk outputs, it does some formatting to display in a nice layout. However, It doesn't actually have all the properties that are in the default output.

To see the properties that actually exist, run:

Get-Disk 0 | Format-List *

The only properties related to size that you're likely to see are AllocatedSize and Size. If you look at the MS doc for the MSFT_Disk CIM class that Get-Disk returns, you'll see that AllocatedSize is "The amount of space, in bytes, that is currently used on the disk." and Size is "Total size of the disk, in bytes."

So, to get something like your output, you'd need something like:

Get-Disk |
Select-Object -Property @{n='Server';e={hostname}},
    @{n='Disk ###';e={'Disk {0}' -f $_.Number}},
    @{n='Status';e={$_.OperationalStatus}},
    @{n='Size';e={'{0:n2} GB' -f ($_.AllocatedSize / 1GB)}}, 
    @{n='Free';e={'{0:n2} GB' -f (($_.Size - $_.AllocatedSize)/1GB)}}, 
    @{n='TotalSize';e={'{0:n2} GB' -f ($_.Size / 1GB)}} |
ConvertTo-Csv -NoTypeInformation
  • Related