1

I am reading a text file. I need to expand the variable in the string. I do not want to run the whole line. It is like I am getting a '' Literal string. I do not what kind of string has "".

  1. make the a text file called C:\temp\testfile.txt.

  2. add the line Thisbrokenit test1 $OnlyExpandThisValue

  3. run the code

    $OnlyExpandThisValue = "It WORKS!"

    $testit = Get-Content C:\temp\testfile.txt

    Invoke-Expression $testit

    $($testit)

I am needing to get the result below.

Thisbrokeit test1 It WORKS!

Aaron
  • 563
  • 3
  • 13
  • 1
    In short: Use `$ExecutionContext.InvokeCommand.ExpandString($string)` to perform string interpolation on demand - but be sure you either fully control or trust the input; see [this answer](https://stackoverflow.com/a/51556308/45375) to the linked duplicate. – mklement0 Dec 14 '20 at 17:53

0 Answers0