Skip to content

Instantly share code, notes, and snippets.

@goncons
Last active January 23, 2023 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goncons/e7b22923faf1528544aed6f124e773ca to your computer and use it in GitHub Desktop.
Save goncons/e7b22923faf1528544aed6f124e773ca to your computer and use it in GitHub Desktop.
OpenFileDialog (with multiple selection) using powershell from visual basic script. This technique could be used show any System.Windows.Forms windows or form.
' Select the file(s) on a directory
' Based on: http://stackoverflow.com/questions/23249895/open-file-dialog-in-vbscript-using-powershell/23251874#23251874
' http://stackoverflow.com/questions/216710/call-openfiledialog-from-powershell/216738#216738
' http://stackoverflow.com/questions/12270667/calling-powershell-with-wshshell-exec-method-hangs-the-script/13518118#13518118''
' Note: In Windows XP when a filter is selected all files/folder disapper,
' when a new directory (parent or other) is selected the filter begins to work normally.
Private Function ConvertFromUTF8(sIn)
Dim oIn: Set oIn = CreateObject("ADODB.Stream")
oIn.Open
oIn.CharSet = "iso-8859-1"
oIn.WriteText sIn
oIn.Position = 0
oIn.CharSet = "UTF-8"
ConvertFromUTF8 = oIn.ReadText
oIn.Close
End Function
Private Function SelectFiles(InitialDir, Filter)
Dim result : result = ""
' Important the property ShowHelp must be $True if not the dialog is not shown
' See: http://stackoverflow.com/questions/216710/call-openfiledialog-from-powershell/216738#216738
With WScript.CreateObject("WScript.Shell").Exec( _
"powershell.exe -NonInteractive -NoProfile -NoLogo -Command ""& {" & _
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;" & _
"[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');" & _
"$objOFDialog = New-Object System.Windows.Forms.OpenFileDialog;" & _
"$objOFDialog.Filter = '" & Filter & "';" & _
"$objOFDialog.InitialDirectory = '" & InitialDir & "';" & _
"$objOFDialog.Multiselect = $True;" & _
"$objOFDialog.RestoreDirectory = $True;" & _
"$objOFDialog.ShowHelp = $True;" & _
"$objOFDialog.SupportMultiDottedExtensions = $True;" & _
"$objOFDialog.Title = 'SelectFiles (" & InitialDir & ")';" & _
"[void]$objOFDialog.ShowDialog();" & _
"$objOFDialog.FileNames -join '|'" & _
"}""")
.StdIn.Close ' Important if not the script hangs:
' See: http://stackoverflow.com/questions/12270667/calling-powershell-with-wshshell-exec-method-hangs-the-script/13518118#13518118
While .Status = 0
WScript.Sleep 100
Wend ' .Status = 0
if .ExitCode = 0 Then
While Not .stdOut.AtEndOfStream
result = result & .stdOut.ReadAll
Wend ' Not .stdOut.AtEndOfStream
' Convert the string to an array of file paths
SelectFiles = Split(Replace(result, vbCrLf, ""), "|")
' Write Write Debug Texto To The Selected Files (Debug):
' Set objFSO=CreateObject("Scripting.FileSystemObject")
' For Each SelectedFile In SelectFiles
' SelectedFilePath = ConvertFromUTF8(SelectedFile)
' Set objFile = objFSO.CreateTextFile(SelectedFilePath, True)
' objFile.Write "Debug Written To Te File """ & _
' SelectedFile & """" & vbCrLf
' objFile.Close
' Next
Else ' .ExitCode <> 0
While Not .stdErr.AtEndOfStream
result = result & .stdErr.ReadAll
Wend ' Not .stdErr.AtEndOfStream
WScript.Echo ConvertFromUTF8(result)
SelectFiles = False
End If ' .ExitCode = 0
End With ' WScript.CreateObject("WScript.Shell").Exec(...)
End Function ' SelectFiles
Dim SelectedFiles
SelectedFiles = SelectFiles("C:\", "Text Files (*.txt)|*.txt|ScriptFiles (*.ps1;*.vbs)|*.ps1;*.vbs|All Files (*.*)|*.*")
If IsArray(SelectedFiles) Then
If UBound(SelectedFiles) >= 0 Then
WScript.Echo "Selected Files: " & ConvertFromUTF8(Join(SelectedFiles, vbCrLf))
Else ' SelectedFiles is Empty
WScript.Echo "NO Files Selected"
End If ' UBound(SelectedFiles) >= 0
WScript.Quit 0
Else ' Not IsArray(SelectedFiles)
WScript.Quit 1
End If ' IsArray(SelectedFiles)
@konkro
Copy link

konkro commented Jan 17, 2023

Hey goncons,

thanks for the script!
Is there a quick way to get this working with UTF-8 characters? Don't know, if this is a Powershell or VBS issue (probably Powershell) but the function cannot handle some German characters that are used quite often, they are missed in the output...

@goncons
Copy link
Author

goncons commented Jan 18, 2023

Hey knkro,

I did tow modifications to the script:

  • Changed the Console Output Encoding to UTF8: [Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
  • Added the function ConvertFromUTF8, to convert the string from UTF8 to be used when the names of the files must be used in VBS.
  • Added the lines from 52 - 60 that allowed the debugging of the names of the files; this lines write the names of the files in the selected files.

I hope this changes solves your problems.

@konkro
Copy link

konkro commented Jan 23, 2023

Hey knkro,

I did tow modifications to the script:

  • Changed the Console Output Encoding to UTF8: [Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
  • Added the function ConvertFromUTF8, to convert the string from UTF8 to be used when the names of the files must be used in VBS.
  • Added the lines from 52 - 60 that allowed the debugging of the names of the files; this lines write the names of the files in the selected files.

I hope this changes solves your problems.

Hello goncons,

this is even more than I've asked for. Thank you!

I managed to get it done in a slightly other way by adding this line to the powershell Command

$OutputEncoding = [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('windows-1252')

As your solution is much better I will ajust my code.
Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment