0

I found a working code here: how-to-get-output-in-gb-instead-of-b

I'm trying to figure out what am I doing wrong & if it could be done this way...

$diskInfo = Get-CimInstance win32_logicaldisk | Select DeviceID, Size, FreeSpace

$diskInfo | foreach-object {
    $computername = "$env:computername"
    $driveLetter = $($_.DeviceID)
    $totalSize = $([math]::round(($_.size /1gb),2))
    $sizeLeft = $([math]::round(($_.freeSpace /1gb),2))
    
    $diskInfo | ConvertTo-Csv -NoTypeInformation
    }

I'm getting the following:

"DeviceID","Size","FreeSpace"

"C:","127389396992","4291960832"

Hoping to get:

"ComputerName","C:","118.64","50" & be able to export it to CSV...

ty

user1245735
  • 43
  • 1
  • 9
  • 1
    You're converting each object to CSV, this line should be removed `$diskInfo | ConvertTo-Csv -NoTypeInformation` and pipe the result of your loop to `| Export-Csv ....` – Santiago Squarzon Mar 10 '22 at 21:06
  • 1
    Actually Theo's answer already shows you how to do it. – Santiago Squarzon Mar 10 '22 at 21:11
  • I removed the line and added } | Export-Csv c:\intel\drives.csv not writing to file... – user1245735 Mar 10 '22 at 21:24
  • 2
    Inside the loop, you are setting values to variables you do not use anywhere. Output **objects** instead, capture the results and export to csv. Just see how it is done [here](https://stackoverflow.com/a/57820888/9898643) – Theo Mar 10 '22 at 21:29

0 Answers0