Powershell: Open File Explorer to select a file

Solved
__Juulien__ Posted messages 20 Registration date   Status Member Last intervention   -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Bonjour,

I would like when executing my script, my program to ask the user to choose a file for the variable $file. With the help of the internet, I found the code below, but I can't get it to work. I execute it as written below:


$file = Select-FileDialog -Title "Select a file"

function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="All Files (*.*)|*.*")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Write-Error "Operation cancelled by user."
}
}

2 answers

yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
 
Hello, "I can't get it to work": what's happening?
Have you tried putting the first line at the end?
1
__Juulien__ Posted messages 20 Registration date   Status Member Last intervention  
 
Yes, the problem is probably the first line. If I don't include it, there are no errors, and if I move it to the end, it doesn't change anything.

Here are my errors when I execute the entire code:

Cannot invoke method. Method invocation is supported only on core types in this language mode.
At C:\Users\FroidevauxJ\Documents\test.ps1:5 char:2
+ [System.Reflection.Assembly]::LoadWithPartialName("System.Windows ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage

New-Object : Cannot create type. Only core types are supported in this language mode.
At C:\Users\FroidevauxJ\Documents\test.ps1:6 char:13
+ $objForm = New-Object System.Windows.Forms.OpenFileDialog
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Object], PSNotSupportedException
+ FullyQualifiedErrorId : CannotCreateTypeConstrainedLanguage,Microsoft.PowerShell.Commands.NewObjectCommand

The property 'InitialDirectory' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:7 char:2
+ $objForm.InitialDirectory = $Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property 'Filter' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:8 char:2
+ $objForm.Filter = $Filter
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property 'Title' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:9 char:2
+ $objForm.Title = $Title
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Users\FroidevauxJ\Documents\test.ps1:10 char:2
+ $Show = $objForm.ShowDialog()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Select-FileDialog : Operation cancelled by user.
At line:1 char:9
+ $file = Select-FileDialog -Title "Select a file"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Select-FileDialog</code>
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
and by executing your script like this, in a command box?
PowerShell.exe -f c:\yourscript.ps1
0
__Juulien__ Posted messages 20 Registration date   Status Member Last intervention   > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
 
Okay, I just understood it's a legal matter, if I execute your command it returns the same errors. You just need to open PowerShell as an administrator.

Thank you for your help
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > __Juulien__ Posted messages 20 Registration date   Status Member Last intervention  
 
Perfect, can you then mark the subject as resolved?
0