This should be a fairly simple question to answer, but I haven't been able to find a resource online. I need to remove rows from a csv
with out changing the filename or path. Currently I am trying to import the file, filter the items I need and export that data to the same location with the same file name:
foreach ($csv in $csvLists){
Import-Csv $csv | Where-Object {[DateTime]$_.'Date' -ge $2WeeksOld} | Export-Csv $csv -NoType -Force
}
Also tried:
foreach ($csv in $csvLists){
Import-Csv $csv | Where-Object {[DateTime]$_.'Date' -ge $2WeeksOld} | Export-Csv $csv.PSParentPath -NoType -Force
}
I am getting access denied errors:
Export-Csv : Access to the path 'D:\Test' is denied.
At C:\script.ps1:20 char:71
+ ... .'Date' -ge $2WeeksOld} | Export-Csv $csv.PSParentPath -NoType -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
What am I missing here? Is the issue that powershell is reading the file and cant export over it at the same time? Should I try setting the imported data as a variable and then exporting the csv outside the import-csv
pipeline?
As always thanks for any help.