1

is there any comlet like expand-string?

$a="ab cd"
$command = 'dir $a'
Write-Host (expand-string $command)

expected:

dir abcd

Note, I know I can just use $command="dir $a" to expand the string, but that was not I want to, I need to save the string dir $a as a literal in a variable and expand it later in a command.

mklement0
  • 382,024
  • 64
  • 607
  • 775
xinglong
  • 115
  • 1
  • 5
  • what are you trying to achieve? not "expand a doublly-embedded string" ... WHY are you wanting to do that? there may be other ways to do it ... [*grin*] ///// have you looked into using a scriptblock instead of a string? if that doesn't work for you, perhaps `Invoke-Command` would work - it RUNS the string instead of simply expanding it. – Lee_Dailey Jul 13 '19 at 20:20
  • 2
    `$ExecutionContext.InvokeCommand.ExpandString($Command)` – iRon Jul 13 '19 at 20:30
  • @iRon, thanks that is what I want. – xinglong Jul 19 '19 at 06:38
  • See https://stackoverflow.com/q/53797850/45375 – mklement0 Feb 02 '20 at 23:49

0 Answers0