Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have used a script from a previous post and modified it slightly:

Get-ChildItem "c:\test\*.txt" | ForEach-Object {          
Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')-$(Get-Date -Format "ddMMyyyy").txt"


Current renamed file: test-ddmmyy.txt
Required Format: testddmmyy.txt

share|improve this question

This ought to work.

Get-ChildItem "c:\test\*.txt" | ForEach-Object {          
Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')$(Get-Date -Format "ddMMyyyy").txt"

Your dash was coming in from just before $(Get-Date. Remember, when passing things in double-quotes as a parameter, anything that's not a variable or is not within parenthesis and preceded by $ will be treated as a string character.

In the below script:

$test='test';"$test-$(Get-Date -F 'ddMMyyyy')"

$test will be expanded into its value 'test', - will be left alone since it is neither a variable nor a part of an expression, and Get-Date -F 'ddMMyyyy' will be processed and return its output since it is set apart as an expression with $(...). This will result in:


Get rid of the dash, and...

$test='test';"$test-$(Get-Date -F 'ddMMyyyy')"


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.