1

I am trying to log "snapshots" of a specified disk drive's usage and capacity on the date/time the script is run. The schema of the table must resemble the following:

   Datetime Drive   Used (GB)   Capacity (GB)
10/3/2022 16:24 C   225.2344    995.5
10/3/2022 16:29 C   223.5344    995.5
10/4/2022 20:34 C   221.8344    995.5
10/6/2022 10:39 C   220.1344    995.5
10/7/2022 04:44 C   218.4344    995.5
10/8/2022 08:49 C   216.7344    995.5
10/9/2022 12:54 C   215.0344    995.5

I want the script to essentially open a CSV file (assuming it exists) and insert a new line at the end of the table (Datetime, Drive, Used (GB), Capacity (GB)).

I've used Get-PSDrive C with Export-CSV to get almost all the information I need, aside from the timestamp. However, I have not found success yet with inserting a new line instead of overwriting the existing data.

Sean P
  • 45
  • 4
  • 1
    You could use `Add-Content` and skip the header using techniques described here: https://stackoverflow.com/questions/26389952/powershell-export-csv-with-no-headers `Get-PSDrive C | convertto-csv | select-object -skip 1 | add-content $myfile` – mjsqu Oct 04 '22 at 00:52
  • 2
    `Export-Csv` has an `-Append` switch. As long as the object being appended has the same properties (columns) as the Csv, it will work just fine. – Santiago Squarzon Oct 04 '22 at 01:00

1 Answers1

1
$datetime = Get-Date -format "yyyyMMdd HH:MM:ss"

Get-PSDrive C | 
  Select-Object @{Name='DateTime';Expression={$datetime}},Name,Used,Free |
  Export-CSV c:\documents\file.csv -NoTypeInformation -Append
mklement0
  • 382,024
  • 64
  • 607
  • 775
Sean P
  • 45
  • 4