here-string with $($var) text

hi all,

I need to create an XML file that contains this line into it;

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>

my script creates the file like so;

$xmlContent = @"

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>


out-file -filepath c:\temp\test.xml -inputobject $xmlContent

when I run it, I get the message;

ERROR: Arg0 : The term ‘Arg0’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
ERROR: included, verify that the path is correct and try again.

so my question: how can I put that line into a here-string to be able to successfully create my file? What is the correct syntax?


These are arguments to a function that you’re not showing us? I’m guessing that would be like:

$($args[0]) $($args[1]) $($args[2])

js is taking a guess, but what is $arg or $args? You need to be careful as ‘args’ is a Powershell keyword. Are the arguments you are inputting space delimited? Here are two methods, which using method 2 is better as you will get errors if the XML is malformed vs method 1 is just manipulating it as text:

$arguments = 'fee','fi','fo', 'fum'

#writing manaully as text
$xmlContent = @”
<?xml version="1.0" encoding="ISO-8859-1"?>
    <arguments>$($arguments -join ' ')</arguments>


#Writing as XML
[xml]$xmlContent = @"
<?xml version="1.0" encoding="ISO-8859-1"?>

$xmlContent.settings.arguments = $arguments -join ' '


well, this was a facepalm moment.

i created my here-string with a single-quote instead of double-quote and it works as expected now. sorry to have bothered you.

$hereString = @’

some text…

<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>

more text


Note: it’s funny though to see that you totally missed the point by automatically assuming i was passing arguments to a script when I only tried to write the entire here-string to a file. :wink:

Unfortunately, assumptions need to be made when only a portion of the code is provided. The error posted indicates the $arg0 variable is not defined and that variable definition was not provided in the post and your provided example has double qoutes. Glad you figured out the issue.

I don’t see how that could possibly work. Is that run through Azure or something?

works like a charm!

$xmlFileContent = @’
<?xml version=“1.0” encoding=“UTF-16”?>
<Task version=“1.6” xmlns=“”>
<Description>Register this computer if the computer is already joined to an Active Directory domain.</Description>
<URI>\Microsoft\Windows\Workplace Join\Automatic-Device-Join-VPN</URI>
<Principal id=“LocalSystem”>
<Actions Context=“LocalSystem”>
<Arguments>$(Arg0) $(Arg1) $(Arg2)</Arguments>
Out-File -FilePath c:\temp\azurejoin.xml -InputObject $xmlFileContent