Since this function is in your profile, I think a global variable with areas is probably the best solution. However, I just wanted to tell you that there is another way.
In other scenarios (not profile functions, etc.) you can avoid global variables, but still there is a function that changes the variable, and the caller has access to this change. In this case, you can create a reference variable and pass it to your function (i.e., Pass by reference).
You would make an $AddedPaths type [ref] , and then pass it as a parameter to your function (now with a variable of reference type):
function AddPath($keys, [ref]$ActivePaths) { if ($keys.Count -eq 0) { return } foreach ($key in $keys) { if ($AddablePaths.Contains($key)) { if (!($ActivePaths.Value -contains $key)) { $env:Path += $AddablePaths[$key] $ActivePaths.Value += $key } } else { Write-Host "Invalid path option. Options are:" foreach ($key in $AddablePaths.keys) { Write " $key" } } } } > [ref]$AddedPaths = @() > AddPath -keys ("ruby","git","notepad++") -ActivePaths $AddedPaths > $AddedPaths Value ----- {ruby, git, notepad++}
To get more help on reference variables:
> help about_ref
source share