Multiple variables in a line without spaces

This seems simple enough, but cant find the right answer.
Customer wants a file delivered, and the file name is all underscores and no spaces. 2 of the names in that filename are dynamic and assigned to variables.

$date = get-date 
$dateBegin = $date.AddDays(-1).ToString('M-dd-yy')
$dateEnd = $date.AddDays(6).ToString('M-dd-yy')
$dest = "c:\temp\this_is_$beginDate_to_$endDate"

So the question is about the quoting. Because theres no spacing in the filename, it tries to treat that as one variable.
How to quote that out to avoid that happening?

Have you tried:

$dest = "c:\temp\this_is_$($beginDate)_to_$($endDate)"

I had not tried that, and feel like a failure now. I was going through my scripts to see if i had done it anywhere else, and doesnt look like i had.

I was on the path of parentheses, and didnt take that last step of having the preceeding $

Thank you for the help though. Sometimes its the little things we get hung up on.

BTW:

That’s called subexpression operator. Here you can read more about:

1 Like

This should also work

Personally, a fan of string format for string concatenation and allows for additional formatting for dates, numbers, timespans directly in the string:

$beginDate = (Get-Date).AddDays(-1)
$endDate = Get-Date
$dest = "c:\temp\this_is_{0:MM---dd---yyyy}_to_{1}" -f $beginDate, $endDate

$dest

Output:

c:\temp\this_is_03---27---2023_to_3/28/2023 11:30:53 AM
2 Likes