Search code examples
powershellcsvpowershell-4.0

Powershell File search by date, display date and path info, cannot export CSV


I'm new to Powershell and I have gotten my batch file to work to output the results to the screen. I want the file exported as a CSV in order to be able to get it into an Excel file sortable by Directory, then date.

I can get the Out-File command to write a basic file, but the CSV output doesn't seem to want to work right. I only get a CSV file with the Length of each record.

Can anyone help? Thanks in advance.


This is what I tried:

#--Change the name with directory you want to visit --#    
$dir_to_look="E:\xPression\CustomerData\xPressionECR\*\XMLDATA"    

#--You may change the number of days of your choice --#   
$TwoDays=$(Get-Date).AddDays(-2)    

Remove-Item E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv

#--Find the files which are modified or created within last 2 days --#    
Get-Childitem $dir_to_look *.xml  -Recurse | `   
        where-object {!($_.psiscontainer)} | `   
        where { $_.LastWriteTime -gt $TwoDays } | `   
        ForEach-Object {  "$($_.LastWriteTime) , $($_.Fullname)"| Out-File -LiteralPath E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv -Append}

Solution

  • Two helpfull functions that you should take a closer look at are Select-Object and Export-Csv.

    If you want to create .csv File that contains two rows consisting of the LastWriteTime and the FullName you can do it like this:

    Get-Childitem $dir_to_look *.xml  -Recurse | Where-Object { $_.LastWriteTime -gt $TwoDays } | Select-Object LastWriteTime, FullName | Export-Csv -LiteralPath "E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv "