Powershell array scope; why is my array empty?


I want to have an array and add elements to it from different functions in my script. My example below illustrates where I might be misunderstanding something regarding scope. My understanding currently is that if an array is defined outside the function, and then elements are added inside the function, those elements should be available outside the function.

What am I missing here? Perhaps my style is completely wrong.


The $services within the function block is scoped to the function. You can do something like the following instead:

Else, you may explicitly use global: to alter the scope:


Powershell array scope; why is my array empty? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply