0

I have the following array:

$webApplications = Get-SPWebApplication | Select-Object Url

which gives output similar to this:

http://testportal.*******de
http://testportal2.******de

I would like the output to be numbered like this for example:

1- http://testportal.*******de
2- http://testportal2.******de

and then store them in variables and have the user be able to choose the portal they want by typing in only the number of the portal. I tried something like this to store them in variables (not sure if it's the right approach):

$Variable1 = $webApplications[0]
$Variable2 = $webApplications[1]
$SelectedURL = Read-Host 'choose your portal by selecting a number'

I just can't seem to figure out how to list the content of the array with numbers on the side and then call it back for the $SelectedURL variable.

1 Answer 1

1

Don't do what you're trying to do. Using dynamic variable names is an anti-pattern.

Do something like this instead:

for ($i=0; $i -lt $webApplications.Count; $i++) {
    '{0} - {1}' -f ($i+1), $webApplications[$i]
}
[int]$SelectedURL = Read-Host 'choose your portal by selecting a number'

$webApplications[($SelectedURL - 1)]
Sign up to request clarification or add additional context in comments.

2 Comments

This gave me the result i was hoping for, thank you very much, if you could maybe explain to me this real quick i'd appreciate it :) ($i=0; $i -lt $webApplications.Count; $i++) { '{0} - {1}' -f ($i+1), $webApplications[$i
The loop is a regular for loop and in the body of that loop I'm using the format operator for creating formatted output.

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.