0

I want the powershell command to call the variable inside the variable ex: PathTom="C:/test/" in foreach statement, I am passing multiple application names like Tom, Web... $app = "Tom" I want to use the path of Tom here, So trying like Write-Output "$Path$app"

what would be the correct command to use?

  • 1
    `(Get-Variable -Name "path$app").Value` – AdminOfThings Sep 17 '21 at 19:07
  • There's also other weird ways like `$executioncontext.InvokeCommand.ExpandString('$path{0}' -f $app)`. – AdminOfThings Sep 17 '21 at 19:15
  • What you're looking for is _variable indirection_, where you refer to a variable _indirectly_, via its name stored in a different variable or provided by an expression. PowerShell enables this via the [`Get-Variable`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-variable) and [`Set-Variable`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/set-variable) cmdlets - see [this answer](https://stackoverflow.com/a/68214332/45375) to the linked duplicate. – mklement0 Sep 17 '21 at 19:50
  • It worked. Thanks – user9616338 Sep 17 '21 at 20:21
  • For the [`$ExecutionContext.InvokeCommand.ExpandString($Path$app)`](https://learn.microsoft.com/powershell/scripting/learn/deep-dives/everything-about-string-substitutions#executioncontext-expandstring) solution, see: [How to Create Text Files from an Array of Values in Powershell](https://stackoverflow.com/a/53797915/1701026) – iRon Sep 18 '21 at 08:26

0 Answers0