I'm trying to write my first PowerShell script and I'm struggling to get my head around the object orientation and piping approach. So I've got a directory structure like this (Google Takeout Nest directories):
Path\DeviceID-A\
- Year\
- Month\
- year-month-sensors-a.csv
Path\DeviceID-B\
- Year\
- Month\
- year-month-sensors-b.csv
...
I want to aggregate all the CSVs together and I've written a simple script that does this:
Get-ChildItem -Recurse -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv merged.csv -NoTypeInformation -Append
However, I lose the context of the DeviceID when everything is aggregated together. So I'd like to add an additional column that contains those initial root directories, with something similar to:
Import-Csv merged.csv | Select-Object *,@{Name='nest_id';Expression={$nestDeviceID}} | Export-Csv merged-v2.csv -NoTypeInformation
So each row in year-month-sensors-a.csv would have an additional column called nest_id with the value DeviceID-A for any CSV under that root directory.
...but I can't quite reconcile the two scripts on how to programmatically iterate through them, whether I need to use ForEach-Object or piping.
Any advice would be appreciated!