In the below sample module file, is there a way to pass the myvar value while importing the module.
For example,
import-module -name .\test.psm1 -?? pass a parameter? e.g value of myvar
#test.psm1
$script:myvar = "hi"
function Show-MyVar {Write-Host $script:myvar}
function Set-MyVar ($Value) {$script:myvar = $Value}
#end test.psm1
(This snippet was copied from another question.)
This worked for me:
You can use the
–ArgumentList
parameter of theimport-module
cmdlet to pass arguments when loading a module.You should use a
param
block in your module to define your parameters:Then call the
import-module
cmdlet like this:As may have already noticed, you can only supply values (no names) to
–ArgumentList
. So you should define you parameters carefully with theposition
argument.Reference