parse result of disk space

Welcome Forums General PowerShell Q&A parse result of disk space

This topic contains 3 replies, has 3 voices, and was last updated by

4 years, 7 months ago.

  • Author
  • #13821

    Points: 0
    Rank: Member

    how can i go over each row in the result and get the drive name and the "FreeSpace(GB)"?
    Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content .\Servers.txt) | Select DeviceID,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}# | Out-GridView

  • #13822

    Points: 0
    Rank: Member

    AS it stands your code outputs:

    Drive Letter (Deviceid)
    Disk Size in GB
    Free Space in GB

    for each hard disk on each machine in your servers.txt file.

    If you only want free space then remove the calculation on Size and change DeviceId to Name – both properties hold the same data

    Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content .\Servers.txt) |
    Select Name,@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

    If you are asking how you can Name in another pipeline step – you can't because select has filtered it out

  • #13834

    Points: 0
    Rank: Member

    If you are working with remote machines across the network – filter as soon as you can – preferably on the remote machine to minimise your network traffic. Only two machines to work with – you won't care. 2000 machines and you'll see a difference. One of the biggest mistakes going round is to drag un-needed data back across the network

  • #13829

    Points: 0
    Rank: Member

    One of the best things about PowerShell is that you almost never have to "parse" anything. Get-WmiObject gives you rich objects with strongly-typed data. Select-Object and Out-GridView (in your code) are fine for formatting the data to be displayed to an end-user, but if you intend to keep doing things with the data afterward, you should just keep a reference to the original objects. For example:

    # Get the Win32_LogicalDisk objects and hold on to them for later use.
    $drives = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content .\Servers.txt)
    # Display some formatted output with Out-GridView
    $drives |
    Select DeviceID,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} |
    # Now you can continue to work with $drives, as needed.

The topic ‘parse result of disk space’ is closed to new replies.