2

Is there a way to combine two string and make it as a current variable and get the data from it? I'm not sure if there's another way to do this. If there's any, please let me know.

Here's my code.

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')
ForEach ($Applications in $Apps) {
    $AppToBeInstalled = '$App' + $_
    $AppExt = '$AppExt' + $_
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

What I want.

Application Google Chrome to be installed with extension .exe
Application Mozilla to be installed with extension .msi

But I'm only getting empty result.

Application  to be installed with extension 
Application  to be installed with extension 

3 Answers 3

2

This is doable, though quite unorthodox, you can use Get-Variable to get variables using dynamic names:

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')

ForEach ($Applications in $Apps) {
    $AppToBeInstalled = (Get-Variable App$Applications).Value
    $AppExt = (Get-Variable AppExt$Applications).Value
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi

What might be a more common approach to this would be to use a hashtable:

$map = @{
    "Google Chrome" = ".exe"
    "Mozilla" = ".msi"
}

ForEach ($key in $map.Keys) {
    $AppToBeInstalled = $key
    $AppExt = $map[$key]
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi
Sign up to request clarification or add additional context in comments.

2 Comments

@Mr.Rak happy to help. Note that, in case you're going to use the Get-Variable method instead of a hashtable, consider using the -ValueOnly switch instead of (Get-Var...).Value as Vojin Purić pointed out in his answer.
Understood. Thank you!
2

Yes you can you can use

Get-Variable -Name "nameOfVar" -ValueOnly

in your case it would look something like

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')
ForEach ($Applications in $Apps) {
    $AppToBeInstalled = Get-Variable -Name "App$Applications" -ValueOnly
    $AppExt = Get-Variable -Name "AppExt$Applications" -ValueOnly
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

1 Comment

This is also working. Thank you so much!
1

As some people mentioned you can create custom variables then call them from within the for loop:

$App1 = [pscustomobject]@{
    Name = "Google Chrome"
    Ext = ".exe"
}
$App2 = [pscustomobject]@{
    Name = "Mozilla"
    Ext = ".msi"
}

$Apps=@($App1,$App2)

ForEach ($App in $Apps) {
    $AppName = $App.Name
    $AppExt = $App.Ext
    Write-Host "Application $AppName to be installed with extension $AppExt"
}

Output will be:

Application Google Chrome to be installed with extension .exe
Application Mozilla to be installed with extension .msi

Might make it easier if you have to keep adding more apps to the script in the future.

2 Comments

This is nice and more easy to follow. Thank you so much!
No problem, glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.