# Disk info tool

**URL:** <https://forums.powershell.org/t/disk-info-tool/5771>\
**Category:** PowerShell Help\
**Created:** [February 12, 2016, 1:09am UTC](https://forums.powershell.org/t/disk-info-tool/5771 "2016-02-12T01:09:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![graham-beer1](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@graham-beer1](https://forums.powershell.org/u/graham-beer1)\
**Post date:** [February 12, 2016, 1:09am UTC](https://forums.powershell.org/t/disk-info-tool/5771/1 "2016-02-12T01:09:12Z")

</div>

Been reading through Ed Wilson’s new book, Step by Step. Some really cool stuff in there and inspired me to have a play !  
Created a small function to obtain the local C: drive freespace and drive size, then format the data into a custom object adding a percent used tab.

Am i on the right lines ?

```
Function Get-DriveInfo { #Runs only for local device

#Get drive information from WMI
$FreeSpace = (get-wmiobject win32_logicaldisk).where{($PSItem.DeviceID -eq 'C:')} | select Freespace,Size

#Create Object to Display Results
$DriveInfo = [pscustomobject]@{
    Freespace = "{0}","GB" -f [math]::truncate($freespace.freespace / 1GB)
    DiskSize = "{0}","GB" -f [math]::truncate($freespace.size / 1GB) 
    PercentUsed = "{0:P}" -f ($freespace.Freespace / $freespace.Size)
    }

#Display 
$DriveInfo | ft -AutoSize

}#End of Function
```

The results are displayed like so,

PS C:\Windows\system32\> Get-DriveInfo

Freespace DiskSize PercentUsed

* * *

48 GB 237 GB 20.25%

Be good to get some feedback, thanks !

---

<div class="post-metadata">

**Author:** ![wilfredo-perez](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wilfredo-perez](https://forums.powershell.org/u/wilfredo-perez)\
**Post date:** [February 12, 2016, 1:48am UTC](https://forums.powershell.org/t/disk-info-tool/5771/2 "2016-02-12T01:48:45Z")

</div>

Nice, but the function only get the C:\drive, how about if I have a d:\drive. Use parameters and advanced functions. Nice script.

---

<div class="post-metadata">

**Author:** ![graham-beer1](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@graham-beer1](https://forums.powershell.org/u/graham-beer1)\
**Post date:** [February 12, 2016, 1:56am UTC](https://forums.powershell.org/t/disk-info-tool/5771/3 "2016-02-12T01:56:32Z")

</div>

Thanks Wifredo, i’ll make it a bit more dynamic.

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [February 12, 2016, 2:04am UTC](https://forums.powershell.org/t/disk-info-tool/5771/4 "2016-02-12T02:04:08Z")

</div>

My only suggestion would be to filter with Get-WmiObject rather than using where-object to filter. This is always good practice, the mantra being ‘filter left and format right’.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [February 12, 2016, 2:18am UTC](https://forums.powershell.org/t/disk-info-tool/5771/5 "2016-02-12T02:18:27Z")

</div>

I’d make these changes:

function Get-DriveInfo {

```
$freespace = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceId = 'C:'" | 
Select-Object Freespace, Size

$props = [ordered]@{
    'Freespace(GB)' = [math]::Round($freespace.freespace / 1GB, 2)
    'DiskSize(GB)' = [math]::Round($freespace.size / 1GB, 2) 
    PercentUsed = [math]::Round(($freespace.Freespace / $freespace.Size) * 100, 2)
}

New-Object -TypeName PSObject -Property $props

```

}

Use the CIM cmdlet rather than WMI cmdlet. Use the -Filter parameter rather than Where (already mentioned above)

Change the properties so that you’re outputting numbers rather than strings - put the units into the property name rather than as part of the value

Output the object. You can always use Format-Table outside the function is need be. PowerShell 5 does a reasonable job of formatting table data automatically

---

<div class="post-metadata">

**Author:** ![graham-beer1](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@graham-beer1](https://forums.powershell.org/u/graham-beer1)\
**Post date:** [February 12, 2016, 2:43am UTC](https://forums.powershell.org/t/disk-info-tool/5771/6 "2016-02-12T02:43:04Z")

</div>

Great stuff, thanks Richard and Peter.  
Richard, any reason why you didn’t use [PSCUSTOMOBJECT] ?

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [February 12, 2016, 6:49am UTC](https://forums.powershell.org/t/disk-info-tool/5771/7 "2016-02-12T06:49:52Z")

</div>

I’ve added advanced function to it…

```
function Get-DriveInfo {

    [CmdletBinding()]
    param (
        [ValidateScript({
            If ($_ -match "^[A-Za-z]\:+$") {
                $True
            }
            else {
                Throw "$_ is either not a valid drive or it is not written as 'C:'."
            }
        })]
        [string]$Drive
    )

$freespace = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceId = '$Drive'" |
 Select-Object Freespace, Size

$props = [ordered]@{
 'Freespace(GB)' = [math]::Round($freespace.freespace / 1GB, 2)
 'DiskSize(GB)' = [math]::Round($freespace.size / 1GB, 2)
 Percentfree = [math]::Round(($freespace.Freespace / $freespace.Size) * 100, 2)
 }

New-Object -TypeName PSObject -Property $props
 }
```

---

<div class="post-metadata">

**Author:** ![michael-hefele](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@michael-hefele](https://forums.powershell.org/u/michael-hefele)\
**Post date:** [February 12, 2016, 6:46pm UTC](https://forums.powershell.org/t/disk-info-tool/5771/8 "2016-02-12T18:46:20Z")

</div>

Hi Graham,

how about setting “C:” as default value for the Drive parameter?

```
[string]$Drive="C:"
```

By doing so, you still have the convenience of your first draft, but also the flexibility of your last version.

Michael

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [February 12, 2016, 10:19pm UTC](https://forums.powershell.org/t/disk-info-tool/5771/9 "2016-02-12T22:19:18Z")

</div>

Nice idea Micheal, thanks !

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/disk-info-tool/5771/10 "2024-05-16T20:42:54Z")

</div>


