this code works as expected:
$var = "a123"
$script = @'
Add-Content C:\temp\test.txt -Value {{"{0} name1"}}
Add-Content C:\temp\test.txt -Value {{"{0} name2"}}
'@ -f $var
$command = [scriptblock]::Create($script)
Start-Process powershell.exe -Verb RunAs -ArgumentList "-NoExit -command & {$command}" -Wait
But when I change the value of $var to "123", I get the error:
At line:1 char:49
+ & {Add-Content C:\temp\test.txt -Value {123 name1}
+ ~~~~~
Unexpected token 'name1' in expression or statement.
At line:2 char:46
+ Add-Content C:\temp\test.txt -Value {123 name2}}
+ ~~~~~
Unexpected token 'name2' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
I tried with other quotes, with casting to string but I have no solution that works. Can anyone help please?