Trouble making a file with the name 20151005_log.txt

Hello Powershell Experts,

I have the following code that returns a date of 20151005:
$ArchiveDate = Get-Date -Format yyyyMMdd


I want to create a file named:  20151005_Log.txt.  I think I would use something like the following, but it is not working:

New-Item -Name '"$ArchiveDate"_log.txt' -Path "$ArchiveFilePath\$ArchiveDate" -ItemType file

What am I doing wrong here?

Thanks,
Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
This should do it:
New-Item -Name "$ArchiveDate`_log.txt" -Path "$ArchiveFilePath\$ArchiveDate" -ItemType file

Open in new window

Note the backtic to escape the underscore.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oBdACommented:
You don't need the name if you provide a full path, and for the log file name, you need a subexpression because the underscore is a legal part of a variable name. The subexpression around $ArchiveFilePath is just there because I like to use it whether it's required or not.
New-Item -Path "$($ArchiveFilePath)\$($ArchiveDate)_log.txt" -ItemType File

Open in new window

Subexpressions are handy for all kinds of manipulation inside strings; this would work, too, for example:
New-Item -Path "$ArchiveFilePath\$(Get-Date -Format yyyyMMdd)_log.txt" -ItemType File

Open in new window

0
ndalmolin_13Author Commented:
Thanks for the quick replies.  Both solutions worked!  Thanks for the info on subexpressions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.