powershell drives calculator

83 Views Asked by At

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
2

There are 2 best solutions below

2
ShanayL On BEST ANSWER

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
0
Lance U. Matthews On

You're overwriting $txtObject on every iteration of the loop, so your output only contains the drive from the final iteration. Instead, you should be initializing $txtObject as an array and then appending each drive's information to that:

$computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3

$txtObject = @()
ForEach($HDD in $computerHDD){
    $txtObject += New-Object PSObject -property @{
        # ...
    } 
}
$txtObject | Select-Object ... | Out-File "$PSSCriptRoot\computer_info.txt" -Append

Better yet, you can eliminate the loop and the variable and just use the pipeline:

$computerName = Get-WmiObject Win32_ComputerSystem
$computerOS = Get-WmiObject Win32_OperatingSystem

Get-WmiObject Win32_LogicalDisk -Filter drivetype=3 `
    | ForEach-Object -Process {
        New-Object PSObject -Property @{
            'ComputerName'  = $computerName.Name
            'ComputerModel' = $computerName.Model
            'SerialNumber'  = $computerName.SerialNumber
            'HDDSize'       = "{0:N2}" -f ($_.Size/1GB)
            'HDDFree'       = "{0:P2}" -f ($_.FreeSpace/$_.Size)
            'OS'            = $computerOS.caption
            'OS_type'       = $computerOS.OSArchitecture
            'User'          = $computerName.UserName
        };
    } | Out-File "$PSSCriptRoot\computer_info.txt" -Append

Note that New-Object above is nearly identical to your original code except $_ has to be used instead of $HDD.