powershelldrives

powershell drives calculator


So I have the below code which works quite well but for some reason it's only calculating my D: drive and not also my C: drive?

$computerName = Get-Wmiobject Win32_ComputerSystem
$computerOS = Get-Wmiobject Win32_OperatingSystem
$computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3

ForEach($HDD in $computerHDD){
$txtObject = New-Object PSObject -property @{
    'ComputerName' = $computerName.Name
    'ComputerModel' = $computerName.Model
    'SerialNumber' = $computerName.SerialNumber
    'HDDSize' = "{0:N2}" -f ($HDD.Size/1GB)
    'HDDFree' = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size)
    'OS' = $computerOS.caption
    'OS_type' = $computerOS.OSArchitecture
    'User' = $computerName.UserName
    } 
}
$txtObject | Select-Object ComputerName, ComputerModel, SerialNumber, HDDSize, HDDFree, OS, Os_type, User | Out-File "$PSSCriptRoot\computer_info.txt" -Append

Solution

  • seems like you would need to make an array. Try this...

    $computerName = Get-Wmiobject Win32_ComputerSystem
    $computerOS = Get-Wmiobject Win32_OperatingSystem
    $computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3
    
    $output = @()
    ForEach($HDD in $computerHDD){
    $txtObject = New-Object PSObject -property @{
        'ComputerName' = $computerName.Name
        'ComputerModel' = $computerName.Model
        'SerialNumber' = $computerName.SerialNumber
        'HDDSize' = "{0:N2}" -f ($HDD.Size/1GB)
        'HDDFree' = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size)
        'OS' = $computerOS.caption
        'OS_type' = $computerOS.OSArchitecture
        'User' = $computerName.UserName
        } 
        $output += $txtObject
    }
    $output | Select-Object ComputerName, ComputerModel, SerialNumber, HDDSize, HDDFree, OS, Os_type, User | Out-File "$PSSCriptRoot\computer_info.txt" -Append