Stultusaur
Stultusaur

Reputation: 35

Outputting Disk Usage in Powershell

I am fairly new to Powershell scripting and I have a task that I need to achieve every week for a large list of clients.

I am looking at outputting the storage usage/capacity on each server.

Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" |
Select-Object -Property DeviceID,
@{L='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}},
@{L="Capacity";E={"{0:N2}" -f ($_.Size/1GB)}}

The above code does what I want, however it outputs it like this:

DeviceID FreeSpaceGB Capacity
-------- ----------- --------
C:       74.91       231.72  

I was hoping it to look more like:

C: 74.91GB/231.72GB

Is it possible to use Select-String to select the output of the above code, and then use that string to give the output I want?

Upvotes: 2

Views: 182

Answers (2)

wasif
wasif

Reputation: 15498

Try this:

Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | ForEach {
  "$($_.DeviceId) $($_.FreeSpace / 1GB)GB/$($_.Size / 1GB)GB"
}

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 59001

You could use a format string to get your desired output:

Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | ForEach-Object {
    '{0} {1:N2}GB/{2:N2}GB' -f $_.DeviceId, ($_.FreeSpace /1GB),($_.Size/1GB) 
}

Upvotes: 2

Related Questions