# Powershell functions and variable scope

**URL:** <https://forums.powershell.org/t/powershell-functions-and-variable-scope/217>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/powershell-functions-and-variable-scope/217 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/powershell-functions-and-variable-scope/217/1 "2011-12-31T18:30:00Z")

</div>

by Lembasts at 2012-10-02 18:39:49

> Greetings,  
> I have several functions which need to update variables defined in the parent scope. One option is to use the prefix like global: before each variable but that looks messy. Alternatively, I could pass the variables as parameters. I thought that parameters that were passed to functions were "by reference" so then whatever I changed in the function would change the parent scope variable value. I wanted to test this with the following code:  
> `function tst($a,$b) {$a++$b++}$a = 0$b = 0tst $a $bwrite-host "$a $b"`  
> However, this writes out zero for both $a and $b.  
> So how can I pass parameters "by reference" please? What am I missing?  
>   
> Cheers  
>   
> David

by poshoholic at 2012-10-02 18:51:14

> Pass by reference like this:  
> `function tst { param( [REF]$a, [REF]$b ) $a.Value++ $b.Value++}$a = 0$b = 0tst ([REF]$a) ([REF]$b)write-host "$a $b"`

by Lembasts at 2012-10-02 18:59:40

> Thanks for your quick reply.  
> This is going to make my code look awfully ugly as I have a number of variables to update and update them many times within the function.  
> I think I might be better off without parameters and using the $script: prefix.  
> Is there any neater solution where you have a function that needs to modify many parent variables many many times?

by Lembasts at 2012-10-02 19:11:04

> I think I found my answer. I can just call my functions with the prefix ". ". So dot-sourcing my functions place them in the same scope as the parent!

by poshoholic at 2012-10-02 19:18:42

> Yes, dot-sourcing works. If you don’t want to dot-source, you could always perform calculations inside a function and set variable values in the parent scope using:  
> Set-Variable -Scope 1 -Name _VariableName_ -Value _NewValue_

by mjolinor at 2012-10-03 19:32:16

> Another option is to replace your variables with a hash table. Variable assignments in a child scope creates a new variable, but assigning values to a hash table entry do not. You can take your existing variable names and make those the key names of a hash table in the parent scope, and then update the values directly within the functions without worrying about scope.

by Lembasts at 2012-10-03 21:21:33

> Thanks. dot-sourcing seems the simplest as I am not only accessing variables but loads of objects and properties defined in the parent.

---

<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:57pm UTC](https://forums.powershell.org/t/powershell-functions-and-variable-scope/217/2 "2024-05-16T20:57:50Z")

</div>

Closed on May 16, 2024, 8:57pm UTC


