Trying to query a group of servers to get used disk space. I have total and free space easily accessed but used is hard to come by. I found the below powershell that works locally can someone help a powershell noob get this to work against a list of servers maybe in a text file and dump the data into excel?

Get-WmiObject Win32_Volume -Filter "DriveType='3'" | ForEach {
            New-Object PSObject -Property @{
                Name = $_.Name
                Label = $_.Label
                FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
                TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
                UsedSpace_GB = ([Math]::Round($_.Capacity /1GB,2)) - ([Math]::Round($_.FreeSpace /1GB,2))
            }
        }
6 Spice ups

Something along these lines?

$Computers = Get-Content C:\blah.txt
$Output = @()

Foreach ($Computer in $Computers)
    {
    $Output += Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName $Computer | ForEach {
                    New-Object PSObject -Property @{
                    Name = $_.Name
                    Label = $_.Label
                    Computer = $Computer
                    FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
                    TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
                    UsedSpace_GB = ([Math]::Round($_.Capacity /1GB,2)) - ([Math]::Round($_.FreeSpace /1GB,2))
                }
            }
    }

$Output | Export-Csv -NoTypeInformation -Path C:\Space.csv

Edit: Sorry missed the Excel requirement initially.

3 Spice ups

Something like this should do what you want.

Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName ( Get-Content C:\Computers.txt )  |
    ForEach-Object {
        [pscustomobject]@{
            Server       = $_.SystemName
            Name         = $_.Name
            Label        = $_.Label
            FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
            TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
            UsedSpace_GB = ([Math]::Round($_.Capacity /1GB,2)) - ([Math]::Round($_.FreeSpace /1GB,2))
            }
        } |
    Export-Csv -Path C:\Output.csv -NoTypeInformation
3 Spice ups

Wow fast responses. Thanks for the help I’ll give this a try when a get a chance and report back.

Get-wmiobject will accept a list of computers. Read all your computer into a variable and use it like so. I read my servers out of AD but could easily be changed to read out of a file.

$OUs = "Domain Controllers", "Servers"
ForEach ($ou in $OUs) {
    $Computers += Get-ADComputer -Filter * -SearchBase "OU=$ou,dc=domain,dc=loc" | select-object -expandproperty dnshostname
}
Get-WmiObject Win32_LogicalDisk -ComputerName $Computers -Filter "DriveType='3'" -ErrorAction SilentlyContinue | ...
2 Spice ups

Thanks for the help this worked like a charm.

1 Spice up

how do I insert into sql table?
thank you.