Author Posts

July 13, 2017 at 9:43 am

Hi Folks,

Need your expert advice here.

Below is the code that I wrote to get the Size and Free space on C and E drives from multiple servers which I would input via a text file.

$Computers = Get-Content -Path c:\temp\servers.txt

    foreach ($Computer in $Computers)

        {
            Get-WmiObject -ComputerName $Computer -Class win32_logicaldisk |
            where {$_.DeviceID -eq "C:" -or $_.DeviceID -eq "E:"} | 
            Select Pscomputername, DeviceID, @{n='Size(GB)'; e={($_.Size/1gb -as [int])}}, @{n='Free(GB)'; e={($_.FreeSpace/1GB) -as [int]}}
        }

Below is the output that I receive.

PSComputerName DeviceID Size(GB) Free(GB)
-------------- -------- -------- --------
Server1        C:             60       31
Server2        C:             50       30
Server3        E:             40       18

Now, I want the Size and Free Space values (Eg. 60 and 31 in first line) to have 'Gb' string with them (Eg. 60Gb and 31Gb).
How to get the output in that format?

July 13, 2017 at 10:20 am

    
foreach ($Computer in $Computers)

        {
            Get-WmiObject -ComputerName $Computer -Class win32_logicaldisk |
            where {$_.DeviceID -eq "C:" -or $_.DeviceID -eq "E:"} | 
            Select Pscomputername, DeviceID, @{n='Size(GB)'; e={("$($_.Size/1gb -as [int])GB")}}, @{n='Free(GB)'; e={("$($_.FreeSpace/1gb -as [int])GB")}}
        }

July 13, 2017 at 11:54 am

Thank you so much DanT.
That worked..! It would be great if you explain me the logic you put those parans () and double quotes ".
I was kind of struggling by putting them here and there, in the same line.

Thanks in Advance.

July 13, 2017 at 2:05 pm

Hi there,

It's just using a subexpression '$()' and then turning the whole thing into a string whilst also adding GB on the end.

Dan

July 13, 2017 at 3:44 pm

The drawback to doing that is that you're turning a numeric value into a string. This could make further processing of your output objects more difficult. Its generally better to set the unit size in the property name and leave the value as a straight numeric