Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
May 16, 2024

Exposing PowerShell Module Scope

In the previous article, I demonstrated how to take advantage of the module scope. This is what allows you to have functions and variables available to commands in your module, but not exposed to the user. I take advantage of this in almost all of my PowerShell modules. To make it clear in my code, I make sure my internal functions use a non-standard name. I haven't worried too much about the variable name. I typically call it what I need it to be. But, I can see a benefit in adding a prefix or some indication to the variable name to make it clear that it is an internal variable.

[version]$__Version = '1.0.0'
> *I opted for the double underscore so I wouldn't mistake it for the pipelined variable $_* Then, in my functions, I can reference this variable.
Write-Verbose "Running module version $__version"
If I see a variable in my session with a double underscore, I know it is an internal variable from a module and I've made a mistake somewhere. Regardless, it wouldn't hurt to add documentation in your commands to make it clear you are referencing an internal variable, including where it is defined. There is also a technique you can use to expose the internal members of your module from your PowerShell session. That's what I want to show you today.
Get a premium subscription for full article and archive access

In the previous article, I demonstrated how to take advantage of the module scope. This is what allows you to have functions and variables available to commands in your module, but not exposed to the user. I take advantage of this in almost all of my PowerShell modules. To make it clear in my code, I make sure my internal functions use a non-standard name. I haven't worried too much about the variable name. I typically call it what I need it to be. But, I can see a benefit in adding a prefix or some indication to the variable name to make it clear that it is an internal variable.

[version]$__Version = '1.0.0'

I opted for the double underscore so I wouldn't mistake it for the pipelined variable $_

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.