Powershell: Open File Explorer to select a file

Solved
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

  • Votes
  • Date
  • Descending date
  1. Contributor
    Hello, "I can't get it to work": what's happening?
    Have you tried putting the first line at the end?
    1. 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>
      1. Contributor
        and by executing your script like this, in a command box?
        PowerShell.exe -f c:\yourscript.ps1
      2. @yg_beOkay, 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
      3. Contributor
        @__Juulien__Perfect, can you then mark the subject as resolved?