Author Posts

December 20, 2017 at 2:38 pm

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!

December 20, 2017 at 2:42 pm

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

December 20, 2017 at 2:51 pm

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

December 20, 2017 at 3:23 pm

OK, because it's Christmas time ... 😉

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

December 20, 2017 at 3:24 pm

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

December 20, 2017 at 4:03 pm

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.