I have this script right here:
$directory = "C:\New Folder"
$counter = 1
Set-Location $directory
#Menu
Write-Host "==========" -BackgroundColor DarkGreen
Write-Host "===MENU===" -BackgroundColor DarkGreen
Write-Host "==========" -BackgroundColor DarkGreen
#Shows only .wav files and it order them
Get-ChildItem -Name -Include *.wav | ForEach-Object {Write-Host "[$counter] " -ForegroundColor Green -NoNewLine; Write-Host "$_"; $counter++}
Write-Host ""
$input = Read-Host -Prompt "Select a file from the menu"
Clear-Host
Write-Host "You have just selected: $input"
At this point i would like to display the name of the selected file. How could i do that?
Although an answer has been accepted, I will demonstrate how you can make a selection out of files using the
forloop.We first have to start by gathering the files using
Get-ChildItemlike so:Notice how I didn't just narrow it down to JUST names? This gives us more options to work with such as, adding the last write time to our selection menu so we can see when they were last worked on.
Next, we will create a
PSCustomObjectto store our results and make a listing out of them using aforloop to assigning a number to each file name based off our gathered items in$WavFiles.If you're familiar on how to make a selection out of an array (
$array[3]), you should be able to peace together whats happening when we iterate though our collection of$WavFilessimply by assigning it, its corresponding array number. The$() | Out-Hostis added due toRead-Hosttaking precedence and displaying before the output of our object; this is a work around for that which you can read more about it here.Lastly, we ask for user input with our
Read-Hostassigning the input value to$inputwhich we will use to once again, reference the corresponding file using the number its assigned in our collection of$WavFilesThe
$inputvariable becomes the number selected which is why we can use it to reference the corresponding value in the array.It all comes together like so:
Output: