Home ยป PowerShell ยป PowerShell Export-CSV with Date in FileName

PowerShell Export-CSV with Date in FileName

The Export-CSV cmdlet in PowerShell is used to create a CSV file from the object passed to it and export CSV to the specified location with a specified append date in the filename.

In this article, we will discuss in using PowerShell how to create a CSV file and export CSV with the date in the filename.

We will use the Export-CSV cmdlet to export CSV and Get-Date cmdlet to get the current date in a specified format to append to the filename.

Export CSV with Date in File Name

Using the Export-CSV cmdlet to convert objects into CSV and export them to CSV file and Get-Date cmdlet in PowerShell to get the current date to add a date in filename while exporting to csv.

Get-Service | Export-Csv -Path D:\LogTest\FTP-02\ServiceList_-$((Get-Date).ToString('dd-MM-yyyy')).csv -NoTypeInformation   

In the above PowerShell script, we have used the Get-Service cmdlet in PowerShell to get the list of services on the computer.

The list of services output passes as input to the Export-CSV cmdlet. Export-CSV uses the Path parameter to specify the file path to export CSV.

Get-Date cmdlet gets the current date in customized format (dd-MM-yyyy) and appends the date to the filename while exporting to CSV.

The Get-Date cmdlet gets the Datetime object, hence before appending the date with the filename, we convert the date to a string using the ToString() function with the format (dd-MM-yyyy).

The output of the above PowerShell script to insert timestamp in the filename is:

PowerShell - Export-CSV Add Date in File Name
PowerShell โ€“ Export-CSV Add Date in File Name

Cool Tip: How to get the last modified file in the directory using PowerShell!

Conclusion

I hope the above article on how to create a CSV file from the objects and export it to CSV with appending date in the file name is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.