Author Posts

October 20, 2017 at 5:18 am

Just a query regarding a module I have which contains a number of simple functions to achieve a simple task (About 8 steps). I have an inline script with the code and it works to achieve this task. I began to break it down into a 'each function' does exactly one job and one job only and calls the next function to achieve this overall task (Put it all in a module).

The reason I am doing this is to improve my knowledge and hopefully distribute it if others where I work find it helpful. It solves a repetitive problem where I work and what I have so far in Powershell saves me a lot of pain

If I am finding that I am using Global variables a fair bit in the module am I heading in the wrong direction or is this acceptable to have 6 global variables for about 9 functions – interested in what those out there who may write 'professional' grade modules to distribute around the work place think. If this is a bad idea, I'll head back to the drawing board to look at how I can redesign

Just looking for some general advice on Global variable use in Modules – thanks

October 20, 2017 at 4:54 pm

I'm sure someone who writes professional grade modules will be along in a minute. Until then you'll have to put up with me 🙂

Mike F Robbins blogged about module scope earlier this year. He goes into more depth, but the short answer is you can use script scope in a module to make a variable accessible to other functions in the module.

function setVar {

    $var1 = "Can't see me"
    $script:var2 = "Peekaboo"


function getVar {

    Write-Output $var1
    Write-Output $script:var2



October 20, 2017 at 11:22 pm

Personally I'd use globally-scoped variables as a last resort. Better, I think, to just pass in any variables your function needs through parameters. There are exceptions to every rule of course, but using parameters gives you more flexibility in general. 6 global variables for 9 small functions seems like alot.