Jeff
Jeff

Reputation: 36573

Copy Variable Value into Script Block

Consider the following

$y = 'foo'
$x = {
    write-host $y
}

powershell -NoProfile -ExecutionPolicy Bypass -Command "$x"

This will not print foo, but I'd like it to. Is there a way to force powershell to inline these variables?

Upvotes: 1

Views: 229

Answers (1)

mjolinor
mjolinor

Reputation: 68273

One option:

$y = 'foo'
$x = [scriptblock]::Create("Write-Host $y")

powershell -NoProfile -ExecutionPolicy Bypass -Command "$x"

Upvotes: 0

Related Questions