Accessing Hidden Module Members

by Sep 18, 2009

Modules in PowerShell v.2 can declare which functions, variables, aliases etc. are public and visible to the caller and which ones are hidden. With a trick, you can also access hidden members. Let's assume you have saved the following module as example.psm1:

$internal = 1
$external = 2
function Test-Function1 {
"I am function 1"

function Test-Function2 {
"I am function 2"

Export-ModuleMember function Test-Function1 -Variable external

You need to import this module with a reference to gain access to hidden members:

$module = Import-Module path_to_module\example.psm1 -passThru

Now, you can submit a script block to the module, which executes in its private context and has access to all members, regardless of what Export-ModuleMember has declared public:

& $module { "The hidden variable INTERNAL contains: $internal }

Twitter This Tip! ReTweet this Tip!