Jvdb
Jvdb

Reputation: 5

Expand array so user can select best option and input into variable

I am trying to figure out how to do the following.

  1. Read a text file with phone numbers in a list that are available each number on a separate line.
  2. Output the available list to the script user to be able to select the best one.
  3. Input the $number into another command to add it to the new user.

I have the following so far

$numbers = gc 'C:\temp\file.txt'

I can output the data by using $numbers[1], $numbers[2], etc., but how can I output this to look good and allow the user to select the best number with all options in the text file? I can do it manually obviously, but if I do 3 x $numbers[1], $numbers[2], $numbers[3] it will miss the numbers 4, 5, 6 etc.

Upvotes: 0

Views: 876

Answers (2)

Esperento57
Esperento57

Reputation: 17472

try Something like this :

$selected=Get-Content "c:\temp\phone.txt" | Out-Gridview -Title "Select your choice" -OutputMode Single

if ($selected)
{
    Write-Host "your choice is $selected" 
}

Upvotes: 2

G42
G42

Reputation: 10019

Something like this:

$numbers = Get-Content 'C:\temp\file.txt' 
$i = 1

foreach($n in $numbers){
    Write-host "Number $i - $n"
    $i++
}

$n = Read-host "Choose a number"

$chosenNumber = $number[$n-1]

Upvotes: 1

Related Questions