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

  • SEARCH
  • Home
  • 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 8144607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:26:01+00:00 2026-06-06T13:26:01+00:00

I have a function that iterates through all HDD’s on a computer, and returns

  • 0

I have a function that iterates through all HDD’s on a computer, and returns information about those drives and their mapping to physical drives in an array.

I would like this function to return the information in a custom object.

Here is the function:

##--------------------------------------------------------------------------
##  FUNCTION.......:  Get-HDDInfo
##  PURPOSE........:  
##  REQUIREMENTS...:  
##  NOTES..........:  
##--------------------------------------------------------------------------
Function Get-HDDInfo {
    [CmdletBinding()]
        Param([Parameter(Mandatory = $True,
            ValueFromPipeLine = $True,
            Position = 0)]
            [String[]]$ComputerName
        )#END: Param
    $W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
    $Array = @()

    $W32_DD | foreach { 
        $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
        + $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition" 
        $Array += $_.Name
        $Array += $_.Model
        <#
        $obj = New-Object PSObject
        $obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
        $obj | Add-Member -MemberType NoteProperty -Name `
            "PDCaption" -Value $_.Name
        $obj | Add-Member -MemberType NoteProperty -Name `
            "PDModel" -Value $_.Model
        $Array += $obj
        #>
        Get-WmiObject -Query $query | foreach { 

            $Array += $_.Name
            $Array += $_.Description 
            $Array += $_.PrimaryPartition

            #$obj = New-Object PSObject
            <#
            $obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPName" -Value $_.Name
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPDescription" -Value $_.Description
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPPrimary" -Value $_.PrimaryPartition
            #>
            $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
            + $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk" 

            Get-WmiObject -Query $query2 | ForEach {
            $Array+= $_.Name
            $Used = [math]::round($_.Size/1024/1024/1024,0)
            $Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
            $Array += [String]$Used +"GB"
            $Array += [String]$Free +"GB"
            #Return $Array;
            #$Array = $Null
            }

            <#
            $Array += $obj
            $obj = $Null
            #>
        }#END: Get-WmiObject -Query
    }#END: $W32_DD | foreach
    ##----------------------------------------------------------------------
    ##  Store results in custom Object
    ##----------------------------------------------------------------------
    Return $Array
}#END: Function Get-HDDInfo

The stuff that is commented out is from my attempts to get the information into a custom object. Maybe I’m just a bit burnt out, but I just can’t seem to make this work right. As you see it, the commented out code tries to overwrite named properties – I knew that when I wrote it, but for some reason I expected it to work anyway 😉

Maybe I shouldn’t work three weeks without a day off, but my brain just isn’t letting me solve this problem.

What I want is to be able to do something like this:

$test = (get-hddinfo $SVR01)
$test.PhysicalDrive1
$test.Partition1
$test.DriveLetter1
$test.TotalSize1
$test.FreeSpace1

This would query a computer named SVR01, and write out the first physical HDD, the first logical partition of that drive, the assigned drive letter, total size of the disk, and free space on the disk.

I could then do something like

$test.PhysicalDrive2
$(same code here for the second physical drive)

What the hell am I doing wrong?

  • 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-06T13:26:02+00:00Added an answer on June 6, 2026 at 1:26 pm

    Try this:

    [CmdletBinding()]
            Param([Parameter(Mandatory = $True,
                ValueFromPipeLine = $True,
                Position = 0)]
                [String[]]$ComputerName
            )
        $W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
    
        $a = new-object  System.Object
        $sc3 = 1
        $sc2 = 1
        $sc1 = 1
        $W32_DD | foreach { 
            $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
            + $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition" 
    
            $a | Add-Member -type NoteProperty -name DiskDriveName$sc1 -value $_.Name
            $a | Add-Member -type NoteProperty -name DiskDriveModel$sc1 -value $_.Model       
    
            Get-WmiObject -Query $query | foreach { 
    
                $a | Add-Member -type NoteProperty -name PartitionName$sc2 -value $_.Name
                $a | Add-Member -type NoteProperty -name PartitionDescription$sc2 -value $_.Description
                $a | Add-Member -type NoteProperty -name PrimaryPartition$sc2 -value $_.PrimaryPartition
    
                $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
                + $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk" 
    
    
                Get-WmiObject -Query $query2 | ForEach {
    
                $a | Add-Member -type NoteProperty -name LogicalDiskName$sc3 -value $_.Name           
    
                $Used = [math]::round($_.Size/1024/1024/1024,0)
                $Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
    
                $a | Add-Member -type NoteProperty -name UsedSpace$sc3 -value $([String]$Used +"GB")
                $a | Add-Member -type NoteProperty -name FreeSpace$sc3 -value $([String]$Free +"GB")
    
                $sc3++
                }
               $sc2++
              }       
           $sc1++
        }
    
        Return $a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a function that searches through my array, and returns all the children
So I have a script that grabs a <tr> and iterates through it to
I have a show function that returns a form, given a document id. The
Stumped by this one: I have a function that returns an array of folders
I have a for loop that iterates through an XML document and finds a
I need to find and iterate through all child elements that have specific attribute.
I have function that has : ob_start(); //Include of some files $content = ob_get_contents();
I need to have function that enables a users to submit a question on
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in

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.