Powershell to list files and have the user select one -
i have folder "d:\prod\transfert" containing list of files.
i want write powershell script list files , let user chose one, script perform actions on selected file. idealy want path of selected file stored in variable.
here output wanted :
>select file : [1] file1.zip [2] file2.zip [3] file3.zip >
all can right listing files no number before :
get-childitem c:\prod\transfert | % { $_.fullname }
thanks !
unless absolutely need console gui, can use out-gridview
let user choose, this:
get-childitem c:\prod\transfert | out-gridview -title 'choose file' -passthru | foreach-object { $_.fullname }
edit ...and store in variable...
$filenames = @(get-childitem c:\prod\transfert | out-gridview -title 'choose file' -passthru)
the @()
wrapper ensures array of filenames returned (even if 1 file or no files chosen).
(the passthru
relies on having powershell 3 or greater)
edit 2
the choice menu below alter display type, depending on whether in console or gui (e.g. ise). haven't tested on winrm, shouldn't spawn gui when called via normal powershell console.
$files = get-childitem -path c:\prod\transfert -file $filechoices = @() ($i=0; $i -lt $files.count; $i++) { $filechoices += [system.management.automation.host.choicedescription]("$($files[$i].name) &$($i+1)") } $userchoice = $host.ui.promptforchoice('select file', 'choose file', $filechoices, 0) + 1 # more useful here... write-host "you chose $($files[$userchoice].fullname)"
be careful of how many of files returned get-childitem
Comments
Post a Comment