stringpowershelldatevariablesconcatenation

PS: Add-Content Get-Date & concatenate variable


I have a variable called BB_NP and I want to add the contents of that (string) to an output text file with a date/time, so just a basic log file.

Add-Content -Path ".\LogFile.txt" -Value $(Get-Date) ($BB_NP)

Every time I try, no matter what combination (wrapping it or not wrapping, etc), it can only do one, and not both, I believe it's due to the two different types of data.

How can I have this write to a text file, the date/time and then concatenate the content of BB_NP variable?.

Thanks!.


Solution

  • Use an expandable string literal, "...":

    Add-Content -Path ".\LogFile.txt" -Value "[$(Get-Date)] $BB_NP"