Andrey Marchuk
Andrey Marchuk

Reputation: 13483

How can I include function from string

I want to do something like:

.Helper.ps1

But I have no helper file. I have code as string.

I would expect something like this:

.$helper

or

 & $helper

Where value of $helper variable is: function Test($test) { $test } But both doesn't work, any ideas? As a workaround I'm saving variable to temp file and load it from there, but it doesn't look too smart.

Thanks Andrey

Upvotes: 0

Views: 63

Answers (1)

Ocaso Protal
Ocaso Protal

Reputation: 20247

Use Invoke-Expression:

$helper = 'function XTest($test) { $test }'
Invoke-Expression $helper
xtest 2

Upvotes: 1

Related Questions