Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export-csv creates an empty csv file

Tags:

powershell

csv

This is my first PowerShell script and I've went over lots of topics but can't seem to find what is wrong. I'm trying to see if files with two extensions are created in a folder 'today' and output filename and date into a csv file.

My code is:

Get-ChildItem 'PATH' -recurse -include @("*.txt*","*.txt.gz") | Where-Object { $_.CreationTime -gt (Get-Date).Date } Select-Object FullName, CreationTime, @{Name="Mbytes";Expression={$_.Length/1Kb}}, @{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | Export-Csv -path 'PATH' -Append

The file gets created, but no data is put into it, even if the info I need is displayed in the PowerShell window when running the code.

like image 855
Alexandru Lazar Avatar asked Feb 05 '26 06:02

Alexandru Lazar


1 Answers

You are missing a |pipeline between the Where-Object and the Select-Object cmdlet:

Get-ChildItem 'PATH' -recurse -include @("*.txt*","*.txt.gz") | 
    Where-Object { $_.CreationTime -gt (Get-Date).Date } | 
    Select-Object FullName, 
        CreationTime, 
        @{Name="Mbytes";Expression={$_.Length/1Kb}},
        @{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | 
    Export-Csv -path 'PATH' -Append
like image 134
Martin Brandl Avatar answered Feb 09 '26 08:02

Martin Brandl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!