Levi Hen
Levi Hen

Reputation: 55

How to perform math in powershell or to be more precise, from a.CSV file

So I'm trying to get the values of my HDD into a CSV with either of the following functions:

This one:

Get-PSDrive -PSProvider FileSystem | select Used,Free,Name,Root | Export-CSV -path "C:\Temp\Output $env:computername\Harddisk $env:computername.csv" -delim ";" -Encoding UTF8

Or this one:

Get-PSDrive -PSProvider FileSystem | Format-Table -AutoSize | Out-String -Width 10000| Out-File -Append "C:\Temp\Output $env:computername\Harddisk2 $env:computername.csv" -Encoding UTF8}

The output of method 1 is in bytes so I will have to perform math on it (divided by 1073741824 seems to get the correct answer) but don't know how.

The output of method 2 is in GBytes (what I want) but it only outputs it into 1 column.

Can anyone tell me how I need to perform math on the 1st or how to put a -delimiter ";" on the 2nd?

Upvotes: 0

Views: 454

Answers (2)

Levi Hen
Levi Hen

Reputation: 55

This is what you need to use if you want to have a "Total" column as well besides the "Used" and "Free" columns.

Get-PSDrive -PSProvider filesystem | select Name, Root, @{n="Used in GB";e={"{0:N2}" -f  ($_.used/1GB)}},   @{n="Free in GB";e={"{0:N2}" -f ($_.Free/1GB)}}, @{n= 'Total in GB' ; e = {"{0:N2}" -f (($_.used + $_.Free)/1GB)}} | Export-CSV -path "C:\Temp\Output $env:computername\Harddisk $env:computername 2.csv" -Delimiter ";" -NoTypeInformation

Upvotes: 0

guiwhatsthat
guiwhatsthat

Reputation: 2434

Here is the solution to your number 1.

Get-PSDrive -PSProvider FileSystem | select Name, Root, @{n="Used in GB";e={[math]::Round($_.Used/1GB,2)}}, @{n="Free in GB";e={[math]::Round($_.Free/1GB,2)}} | Export-CSV -path "C:\Temp\test1.csv" -Delimiter ";" -NoTypeInformation

Upvotes: 1

Related Questions