Home > Software engineering >  How to export variables from a PowerShell module
How to export variables from a PowerShell module

Time:08-05

I've defined a variable in a psm1 file but when I try to access it in another script, after importing the module, I'm not seeing the value set in the psm1 file.

globals.psm1

$blah = "hello world"

my-script.ps1

Import-Module "$PSScriptRoot\globals.psm1" -Force -Verbose
Write-Output "blah: ${blah}"

output

PS C:\blah> .\my-script.ps1
VERBOSE: Loading module from path 'C:\blah\globals.psm1'.
blah: ''

I thought all variables get exported by default. I must be interrupting this wrong:

Specifies the variables that the module exports to the caller's session state. Wildcard characters are permitted. By default, all variables ('*') are exported

source: MSFT Docs -> How to write a PowerShell module manifest
(CTRL F on 'VariablesToExport' to find the quoted text)


And yes, if I export the variable, I can access it but the documentation says: 'By default, all varialbes ('*') are exported so what am I doing wrong or misunderstanding?

  • Related