# Powershell Module - variable Scopes - general advice

**URL:** <https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563>\
**Category:** PowerShell Help\
**Created:** [October 19, 2017, 11:48pm UTC](https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563 "2017-10-19T23:48:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew-rasdell](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@andrew-rasdell](https://forums.powershell.org/u/andrew-rasdell)\
**Post date:** [October 19, 2017, 11:48pm UTC](https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563/1 "2017-10-19T23:48:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 20, 2017, 11:24am UTC](https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563/2 "2017-10-20T11:24:06Z")

</div>

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](http://mikefrobbins.com/2017/06/08/what-is-this-module-scope-in-powershell-that-you-speak-of/) 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

}

setVar
getVar
```

---

<div class="post-metadata">

**Author:** ![jmmurrah](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jmmurrah](https://forums.powershell.org/u/jmmurrah)\
**Post date:** [October 20, 2017, 5:52pm UTC](https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563/3 "2017-10-20T17:52:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/powershell-module-variable-scopes-general-advice/9563/4 "2024-05-16T20:37:22Z")

</div>


