how can i format the output in a powershell script from this command? When i just type it in the console it sorts nicely but in the script it just throw all in one row
$diskpartitions = Get-Partition |Select PartitionNumber, DriveLetter, Size, DiskId | Sort-Object DiskId Write-Host $diskpartitions
CodePudding user response:
Write-Host
converts any input given to it into the form of a string. A string has to take a nicely formatted PowerShell object and convert it into, well, a string.
You should use Write-Output
instead, which will render your object as you have it.
PS>Write-output $diskpartitions
PartitionNumber DriveLetter Size DiskId
--------------- ----------- ---- ------
1 16777216 \\?\scsi#disk&ven_nvme&prod_samsung_ssd_960#5&17cb1da0&0&000000#{53f56307-b6…
2 C 499537713664 \\?\scsi#disk&ven_nvme&prod_samsung_ssd_960#5&17cb1da0&0&000000#{53f56307-b6…
3 550502400 \\?\scsi#disk&ven_nvme&prod_samsung_ssd_960#5&17cb1da0&0&000000#{53f56307-b6…
1 134217728 \\?\scsi#disk&ven_nvme&prod_samsung_ssd_960#7&1a97d747&0&000000#{53f56307-b6…
2 V 499971522560 \\?\scsi#disk&ven_nvme&prod_samsung_ssd_960#7&1a97d747&0&000000#{53f56307-b6…
1 W 1000202043392 \\?\scsi#disk&ven_samsung&prod_hd103sj#5&1bc941f&0&070000#{53f56307-b6bf-11d…
1 471859200 \\?\scsi#disk&ven_samsung&prod_ssd_850_pro_256g#5&1bc941f&0&010000#{53f56307…
2 103809024 \\?\scsi#disk&ven_samsung&prod_ssd_850_pro_256g#5&1bc941f&0&010000#{53f56307…
3 16777216 \\?\scsi#disk&ven_samsung&prod_ssd_850_pro_256g#5&1bc941f&0&010000#{53f56307…
4 D 255466668032 \\?\scsi#disk&ven_samsung&prod_ssd_850_pro_256g#5&1bc941f&0&010000#{53f56307…
1 G 1000202043392 \\?\scsi#disk&ven_samsung&prod_ssd_860_evo_1tb#5&1bc941f&0&060000#{53f56307-…
1 134217728 \\?\scsi#disk&ven_seagate&prod_backup _hub_bk#8&2a5cdee5&0&000000#{53f56307-…
2 H 6001039245312 \\?\scsi#disk&ven_seagate&prod_backup _hub_bk#8&2a5cdee5&0&000000#{53f56307-…
Fun protip
Every line of PowerShell code has an implicit Write-Output
call at the end. You can just echo out the contents of a variable by having the variable name by itself on a line, as an easy alternative.
PS> $diskpartitions
PartitionNumber DriveLetter Size DiskId
--------------- ----------- ---- ------
1 16777216 \\?\scsi#disk&ven_nvme&prod_sa
#...same content as before