Search This Blog

Friday, November 22, 2013

Adding date stamp to filename using Powershell

The previous post was about adding the date stamp to files that were already created. This script would prevent you from having to do it again. If having the datestamp is necessary in the filename, why not put it in when you create the file.

The script below will save the date as you create the file

Get-Process | Export-Csv test_$((Get-Date).ToString('ddMMMyy')).csv

In this example test is the name of the file and $((Get-Date).ToString('ddMMMyy')) puts the the date after in the format of  Day Month Year

Output would look like test_22Nov13.csv

Note: You need to change the name "test" to something meaningful to the file being saved  or you could pass the variable to be saved.

$Filename = read-host "what would you like to call the file"
$Computername =  read-host "Which Computer"
Get-Process -ComputerName $Computername  | Export-Csv ($Filename.ToUpper() +"_"+ $((Get-Date).ToString('ddMMMyy')) +".csv")

No comments:

Post a Comment