Vaccano Vaccano - 1 year ago 82
PowerShell Question

Use a variable in a command

This is part of the PowerShell script I am working on:

Write-Host $configJson.myVal
(Get-Content .\config.js) -replace "S=''", "S='$configJson.myVal';" | Set-Content .\out.js

part correctly displays the value in

But when I run the second statement, the value that is put in the file is:

How can I change the second command so that the value that is output on the
line is also put into the file for my replace command?

Answer Source

I would use a format string:

Write-Host $configJson.myVal
(Get-Content .\config.js) -replace "S=''", ("S='{0}';" -f $configJson.myVal) | Set-Content .\out.js
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download