1

Team - using the following snippet of code

Export-Csv $destinationpath\$destinationfilename_$(get-date -Format yyyymmdd_hhmmtt).csv -NoTypeInformation

I'm using a variable for the file location ($destinationpath) and another variable for the file name ($destinationfilename) ... and then just adding a timestamp for tracking.

The file will export to the correct location however the file name portion ($destionalfilename) is not included ... just the timestamp. Looks like this in the folder: 20212511_0225PM

I can see the variable stored in PowerShell but it just won't make it into the file name. I've played around with single and double quotes ... spacing and backslashes in the variable names.

Suggestions?

1
  • Note: The linked post isn't an exact duplicate in terms of the question, but the accepted answer there discusses in more detail under what circumstances the syntax form ${...} is required, which complements js2010's helpful answer. Commented Aug 11, 2021 at 22:12

1 Answer 1

4

An underline can be part of a variable name, so it's looking for $destinationfilename_, which is null. You can add the curly braces to make it clear. A dash after the variable name wouldn't have the same problem.

Export-Csv $destinationpath\${destinationfilename}_$(get-date -Format yyyymmdd_hhmmtt).csv
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.