Powershell Add-Content

36,162

Solution 1

Try "This is my text $(Get-Date)"

In PowerShell, strings in double quotes can contain variables and expressions. If it's not a simple expression (e.g. "This is a $value"), then you need to wrap the expression in $() (e.g. "This is a $($value + 1)").

Note that strings in single quotes are "verbatim strings" and do not allow escape characters or expressions.

Solution 2

I prefer MohitC method but you can also enclose your value in parenthesis:

Add-content -path $logfile -value ('This is my text '+(Get-Date))
Share:
36,162
Admin
Author by

Admin

Updated on September 28, 2020

Comments

  • Admin
    Admin over 3 years

    I can't get add-content to add both a string and the output of a cmdlet so it would look something like this;

    Add-content -path $logfile -value "This is my text"+(Get-Date)
    

    I realise I can just add another line to set a variable to the result of get-date but and then pass the variable into my add-content command but I just wondered if I could do it in a single line, like a said Im being anal lol

    Cheers

    Andy