2

I've found this function that I use to get the full path to a file:

Function Get-Filename($initialDirectory="")

{

    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
 
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialdirectory
    # $OpenFileDialog.filter = "TXT (*.txt)| *.txt"
    $OpenFileDialog.ShowHelp = $true
    $OpenFileDialog.ShowDialog() | out-null
    $OpenFileDialog.filename
  
    return $filnavn 
}

$InputFil = Get-FileName

$InputFil

$OurFilesData = Get-Content $InputFil -Encoding UTF8

I get this error message:

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:27 char:29
+ $OurFilesData = Get-Content $InputFil -Encoding UTF8
+                             ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

What I know / has tried:

  • I'm running the code locally on my own machine
  • $InputFil returns the correct full path and filename (example: C:\Temp\E-Drevet_KANALNAVNE.CSV)
  • If it set the $InputFil variable manually ($InputFil = "C:\Temp\E-Drevet_KANALNAVNE.CSV") I don't get the same error
  • Power Shell Version 5.1.19041.546

Why do I get this error when the input seems to be the same as when I manually set the $InputFil variable?

3
  • Did you tried Get-Content -Path $InputFil? Commented Nov 10, 2020 at 14:33
  • 1
    The error in your post doesn't seem to match the title, can you double check and clarify? Commented Nov 10, 2020 at 14:33
  • 1
    Remove return $filnavn Commented Nov 10, 2020 at 14:33

1 Answer 1

4

The error is thrown because one of the elements in $InputFil is $null.

Let's take a look at the last two statements in the function:

    $OpenFileDialog.filename
  
    return $filnavn 

PowerShell outputs everything, not just whatever expression follows the return statement - so here, PowerShell first outputs the string value of $OpenFileDialog.filename, and then it subsequently outputs $filnavn - but $filnavn it never assigned to, so it resolve to $null.

The result is that $InputFil now holds an array, basically $InputFil = @("C:\actual\file\path.ext",$null)

Change your function definition to:

function Get-Filename($initialDirectory="")
{

    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
 
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialdirectory
    # $OpenFileDialog.filter = "TXT (*.txt)| *.txt"
    $OpenFileDialog.ShowHelp = $true

    if($OpenFileDialog.ShowDialog() -eq 'OK'){
      return $OpenFileDialog.filename
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Worked like a champ. Thank you so much. I've upvoted you, but since I'm new it's not visible (?!?)
@RobertGrøndahlWinther That's great, you're welcome! FWIW you need at least 15 rep to upvote, don't sweat it :)
Upvote by proxy at your service. Have a great day. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.