Read-Host with limited values

Hello everyone, basically I have a script that is working in my AD environment, I search for computers/server based on specific criteria and put them into a variable, as a result, I want to be able to present to user values from variable, user should be able to pick one of the values and script will after operate on that value, let’s bring one example.

 $Serverlist = (Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Enabled -eq $True}).Name 

I want to be able to present values that are stored in $Serverlist variable to the user with Read-Host cmdlet, for example;

 $Answer = Read-Host "Pick one of the servers" 
But I do not know how to present possible values stored in the variable to user. Any help will be appreciated. Thanks!

You could output your server list including an index and only ask for the index number to be provided by the user. :wink:

Hi Olaf, thanks for quick answer, can you please guide me with one example?

OK, because it’s Christmas time … :wink:

$ServerList = Get-ADComputer -Filter {OperatingSystem -like “Server” -and Enabled -eq $True}
$ServerList | Select-Object -Property @{Name=‘Index’;Expression={$ServerList.Indexof($_)}},Name

Thanks dude, much appreciated, happy Christmas to you and your family :slight_smile: cheers!

A much easier way - use Out-GridView with the -PassThru switch:

$Serverlist = (Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Enabled -eq $True}).Name | Out-GridView -PassThru

You’ll get a pretty popup with all of the entries. You can select whichever you want (including holding Ctrl to select multiple), then click OK on the bottom right. The entries that you selected will be saved to $Serverlist.