Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:49:35+00:00 2026-06-17T18:49:35+00:00

Below is the code for checking the disk space: $disks = Get-WmiObject -ComputerName WDSMS01

  • 0

Below is the code for checking the disk space:

$disks = Get-WmiObject -ComputerName WDSMS01 -Namespace root\cimv2 -Query "Select * from Win32_LogicalDisk where DriveType=3"

$disks.DeviceID
$dsql01 = @()

foreach($db in $disks)
{
    $dsql = New-Object PSObject
    $dsql | Add-Member -MemberType NoteProperty -Name "Drive Name" -value $db.DeviceID
    $dsql | Add-Member -MemberType NoteProperty -Name "Total Space(GB)" -value ($db.Size/1GB)
    $dsql | Add-Member -MemberType NoteProperty -Name "Free Space(GB)" -value ($db.FreeSpace/1GB)
    $dsql01 += $dsql
}

$dsql01

Is it possible to limit the decimals by 2 digits? Also, add another column that computes free space in % format.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:49:36+00:00Added an answer on June 17, 2026 at 6:49 pm

    This depends a bit on what exactly you need. You can use [Math]::Round to round off everything after two digits:

    [Math]::Round($db.Size/1GB, 2)
    

    but that can leave you with numbers that have less than two digits if they are followed by zeroes, e.g. 14.6 which doesn’t look as nice in a table.

    The other option is to format the numbers:

    ($db.Size/1GB).ToString('0.00')
    

    which will always use the same number of decimal digits, but results in a string, so performing further calculations can be hindered by that.

    As for your second question, that’s actually trivial, just add another Add-Member call:

    $dsql | Add-Member NoteProperty 'Free Space (%)' (100*$db.FreeSpace/$db.Size)
    

    Also you can (in PowerShell v2) put all those in a single New-Object call:

    $dsql = New-Object PSObject -Property @{
        'Drive Name' = $db.DeviceID
        'Total Space (GB)' = $db.Size / 1GB
        'Free Space (GB)' = $db.FreeSpace / 1GB
        'Free Space (%)' = 100 * $db.FreeSpace / $db.Size
    }
    

    And while we’re at it, foreach loops are so not PowerShell-y. Let’s rewrite that with a pipeline:

    $disks = Get-WmiObject -ComputerName WDSMS01 -Namespace root\cimv2 -Query "Select * from Win32_LogicalDisk where DriveType=3"
    $disks.DeviceID
    $dsql01 = $disks | ForEach-Object {
        New-Object PSObject -Property @{
            'Drive Name' = $_.DeviceID
            'Total Space (GB)' = $_.Size / 1GB
            'Free Space (GB)' = $_.FreeSpace / 1GB
            'Free Space (%)' = 100 * $_.FreeSpace / $_.Size
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used below code for checking lastname(case sensitive) from DB. SELECT * FROM
how can i get multiview in javascript or jquery ? below code always returns
As in the simplified code below, is checking that SendAsync() has finished transmitting all
I have got below code to GET dictionary type of session variable value. Please
I was having below code in VbScript for checking the valid navigation Item. '==============================================
For my custom made chat screen i am using the code below for checking
i am checking networking connection using the below code: public static boolean haveInternet(Context ctx)
I am using code below. How Do add error checking. If anything is error,
The code below is checking performance of three different ways to do same solution.
Im trying to get a hit test/overlap test to work in the below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.