Trying to open VBS via Powershell using System.Windows.Forms

Hi,

I’m trying to run function in my script, that would open window to choose file, that should be opened via powershell.

To do this, I’m using this script: https://blogs.technet.microsoft.com/heyscriptingguy/2009/09/01/hey-scripting-guy-can-i-open-a-file-dialog-box-with-windows-powershell/

Problem is, that after I run my script, window opened as it should, but after I choosed specific .vbs file, nothing happened.I searched for solution and found only one thing – that I should add $OpenFolderDialog.ShowHelp = $true; but it helped only with running it in Powershell instead of Powershell ISE.

I’m trying to do this in Powershell 2.0 to make sure that this script will run on Win 7 and never OSes.

My code:

Function RunVBS($initialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "VBS (*.VBS)| *.VBS $OpenFileDialog.Showhelp = $true $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename }

I’m trying to run it via command:

RunVBS -initialdirectory

My only clue is that $OpenFileDialog.filename would work in Powershell 3.0 and higher, but hadn’t got idea how to replace that line.

Any ideas?

submitted by /u/yoGhurrt1
[link] [comments]

Leave a Reply