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?

Matthew
  • 111
  • 7
  • 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. – mklement0 Aug 11 '21 at 22:12

1 Answers1

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
js2010
  • 23,033
  • 6
  • 64
  • 66