1

I am trying to run a simple script, which contains Logical Disk's FreeSpace and Size information, though the output of this is not what i am trying to accomplish. I wont list the full script since it's irrelevant, I am going to list just these two commands.

Here is the Script:

    Get-WmiObject -Class win32_logicaldisk | select -Property Name , @{n='HDDs Total Size In GB';e={[math]::Round($_.size/1gb)}}


Get-WmiObject -Class Win32_logicalDisk | select -Property Name , @{n='HDDs FreeSpace';e={[math]::Round($_.FreeSpace/1gb)}}

This is the Output:

    Name HDDs Total Size In GB
---- ---------------------
C:                     111
D:                     932
E:                     932
C:                        
D:                        
E:  

As you can see it leaves the Free space empty.

Any help is appreciated.

Tamer SL
  • 25
  • 1
  • 6

2 Answers2

1

If there's no particular reason to calling it twice you can get the free space in just one:

Get-WmiObject -Class win32_logicaldisk | select -Property Name , @{n='HDDs Total Size In GB';e={[math]::Round($_.size/1gb)}}, @{n='HDDs FreeSpace';e={[math]::Round($_.FreeSpace/1gb)}}

This is my output:

Name HDDs Total Size In GB HDDs FreeSpace
---- --------------------- --------------
C:                     146              6
E:                     205             35
Fabian Mendez
  • 512
  • 5
  • 15
1

Fabian Mendez's helpful answer bypasses the problem by providing a single-command solution.

As for your symptom:

As you can see it leaves the Free space empty.

No, it simply doesn't print the HDDs FreeSpace column to the console - though that column (properties by that name on the output objects) still exists, if you were to examine the output programmatically.

In short, your first Get-WmiObject ... | Select-Object command locks in the display columns, which are Name and HDDs Total Size In GB.

While the second command's output has a Name column, it lacks a HDDs Total Size In GB column, so no values show up for it - and the HDDs FreeSpace column is ignored altogether.

This behavior, which relates to implicit use of the Format-Table output-formatting cmdlet, is explained in detail in this answer of mine.

To force separate output formatting for each command, use ... | Out-Host or ... | Format-* (formatting cmdlets such as Format-Table).

Caveat: Do not do this if you need to process the output programmatically, because Out-Host bypasses PowerShell's success (data) output stream; while using Format-* cmdlets would still send data to the success stream, this data is no longer the input objects, but rather objects representing formatting instructions.


Simplified example:

PS> [pscustomobject] @{ one=1; two=2 }; [pscustomobject] @{ one=10; three=30 } 

one two
--- ---
  1   2
 10    

Note how column two is blank for the 2nd object and its three column doesn't print at all, because only the 1st output object's properties determined the output columns.

Through explicit use of Out-Host or a formatting cmdlet you can force the two commands to be formatted individually:

PS> [pscustomobject] @{ one=1; two=2 } | Out-Host; [pscustomobject] @{ one=10; three=30 } 

one two
--- ---
  1   2



one three
--- -----
 10    30

Note how each object's columns now show up, preceded by an object-specific header.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    This is very helpful, I managed to make something work out of this and it worked just fine, in fact this helped me with other scripts which i have a similar issue with, I appreciate it ! – Tamer SL Feb 06 '18 at 10:16