I want to add today’s date to a file name<\/p>\n
$Date = Get-Date -Format \"MMddyyyy\"\n<\/code><\/pre>\n
Advertisement
How do I append that to a file name? I’ve tried<\/p>\n
$file=Get-Item $scriptPath\\*.csv\nRename-Item $file $file_$Date.csv\n<\/code><\/pre>\n
Advertisement
But that just renames the file with the date. How do I take the existing name and add $date to it? ($file will only point to one file name, but the file name is random, that’s why I do it that way)<\/p>\n
Thanks<\/p>","upvoteCount":3,"answerCount":9,"datePublished":"2025-05-28T18:54:39.813Z","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"},"acceptedAnswer":{"@type":"Answer","text":"
Try this instead:<\/p>\n
Rename-Item $file \"$($file)_$Date.csv\"\n<\/code><\/pre>\nYou could also do this (though it makes a call to Get-Date each time which might not be good in a loop):<\/p>\n
Rename-Item $file \"$($file)_$(Get-Date -Format MMddyyyy).csv\"\n<\/code><\/pre>\nEdit (further explanation): Basically, when the second argument in the Rename-Item cmdlet was being parsed, it was seeing the underscore as part of a variable name (since it’s a valid character in a variable name) instead of as just another character between two separate variables. Since $file_ is not defined, it would evaluate to null and appends the remainder of the expression to it which is why you would only get a .csv file with the date as the name.<\/p>","upvoteCount":2,"datePublished":"2025-05-28T19:09:53.135Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/2","author":{"@type":"Person","name":"merlinyoda","url":"https://community.spiceworks.com/u/merlinyoda"}},"suggestedAnswer":[{"@type":"Answer","text":"
I want to add today’s date to a file name<\/p>\n
$Date = Get-Date -Format \"MMddyyyy\"\n<\/code><\/pre>\nHow do I append that to a file name? I’ve tried<\/p>\n
$file=Get-Item $scriptPath\\*.csv\nRename-Item $file $file_$Date.csv\n<\/code><\/pre>\nBut that just renames the file with the date. How do I take the existing name and add $date to it? ($file will only point to one file name, but the file name is random, that’s why I do it that way)<\/p>\n
Thanks<\/p>","upvoteCount":3,"datePublished":"2025-05-28T18:54:39.923Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/1","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
I have also accomplished this by concatenating strings together.<\/p>\n
For example, something like this:<\/p>\n
\n$Date = Get-Date -Format “MMddyyyy”
\n$name = “name_of_file”
\n$filename = $name + $date + “.csv”<\/p>\n<\/blockquote>","upvoteCount":2,"datePublished":"2025-05-28T19:33:39.798Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/3","author":{"@type":"Person","name":"c-t","url":"https://community.spiceworks.com/u/c-t"}},{"@type":"Answer","text":"
Get-Item is returning an object, so you can’t just rename the file that way.<\/p>\n
$Date = Get-Date -Format \"MMddyyyy\"\n$FileList=Get-Item $scriptPath\\*.csv\n$FileList | foreach {Rename-Item $_.fullname \"$_.fullname-$Date\"}\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2025-05-28T19:35:58.653Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/4","author":{"@type":"Person","name":"PatrickFarrell","url":"https://community.spiceworks.com/u/PatrickFarrell"}},{"@type":"Answer","text":"Good catch.<\/p>\n
Wouldn’t this:<\/p>\n
$FileList | foreach {Rename-Item $_.fullname \"$_.fullname-$Date\"}\n<\/code><\/pre>\njust put the date at the very end of the file name (even after the extension)?<\/p>\n
I checked into some of the file object properties and it looks like this is more along the lines of what the OP is looking for:<\/p>\n
$FileList | foreach {Rename-Item $_.fullname \"$_.directoryname\\$($_.basename)_$Date$_.Extension\"}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-05-28T20:03:58.626Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/5","author":{"@type":"Person","name":"merlinyoda","url":"https://community.spiceworks.com/u/merlinyoda"}},{"@type":"Answer","text":"Absolutely right, I missed that.<\/p>","upvoteCount":2,"datePublished":"2025-05-28T20:20:49.507Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/6","author":{"@type":"Person","name":"PatrickFarrell","url":"https://community.spiceworks.com/u/PatrickFarrell"}},{"@type":"Answer","text":"
There are some interesting things to note here.<\/p>\n
$file will contain System.IO.FileInfo objects. The ToString method for that class returns the same as FullName contents. So you can simply just quote the variable “$file” (if it is a single item or quote the current item during iteration) rather than $file.FullName.<\/p>\n
Rename-Item is a good candidate for delay-bind script block. That would make this rename process concise and more inline, especially since you aren’t changing directories.<\/p>\n
$file | Rename-Item -NewName { $_.BaseName + \"_$Date.csv\" }\n# or if you don't like concatenation\n$file | Rename-Item -NewName { \"$($_.BaseName)_$Date.csv\" }\n<\/code><\/pre>\nWhy does the above work? Well, the -NewName parameter is the correct, assigned object type. $file contains the PsPath property, which binds to -LiteralPath in Rename-Item. Then you pass the scriptblock to -NewName and the delay bind will execute the block after the pipeline element is sent through rather than when the command is executed.<\/p>","upvoteCount":2,"datePublished":"2025-05-28T20:56:20.537Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/7","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
It’s little things like this that I always find interesting and, occasionally, frustrating about PowerShell. There are often numerous ways to do basically the exact same thing but just a little bit differently thanks to things like implicit calls to an object’s ToString function simply by quoting the variable containing it. It’s definitely the sort of thing you just don’t get in complied languages (which tend to be strongly typed anyway).<\/p>","upvoteCount":0,"datePublished":"2025-05-28T21:40:41.900Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/8","author":{"@type":"Person","name":"merlinyoda","url":"https://community.spiceworks.com/u/merlinyoda"}},{"@type":"Answer","text":"
I think it is more readable to access the member FullName anyway. I just wanted to add my two cents even if it’s worth a half cent.<\/p>","upvoteCount":1,"datePublished":"2025-05-28T23:51:54.678Z","url":"https://community.spiceworks.com/t/appending-the-date-to-a-file-name/1210290/9","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}}]}}