Skip to content

$Context.CurrentFile.GetText not compatible with $Context.SelectedRange #1496

Closed

Description

In the Powershell 5.1 version, the following code do not work:

$Context = $psEditor.GetEditorContext()
$Context.CurrentFile.GetText($context.SelectedRange)

This seems to be a type error

Cannot find an overload for "GetText" and the argument count: "1".
At line:1 char:1
+ $text = $Context.CurrentFile.GetText($context.SelectedRange)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Solution is to do the following

    $Context = $psEditor.GetEditorContext()
    $Range = [Microsoft.PowerShell.EditorServices.Extensions.FileRange]::new($context.SelectedRange.Start,$context.SelectedRange.End)
    $Context.CurrentFile.GetText($Range)

I would expect the $Context.SelectedRange to work directly with the GetText method which take a FileRange as argument.

That does not end there.
When working with Powershell 7.2, the Microsoft.PowerShell.EditorServices.Extensions.FileRange type does not exist so not only the way you'd expect it to work does not work but also the workaround of creating a new object of the aforementionned type manually and pasting value from the context range do not work either.

The error in a PS 7,2 VSCode session with GetText and $Context.SelectedRange

MethodException: Cannot find an overload for "GetText" and the argument count: "1".

The error in a PS 7.2 VSCode session when trying to create a filerange object manually

InvalidOperation: Unable to find type [Microsoft.PowerShell.EditorServices.Extensions.FileRange].

Activity

  1. ghost added
    Needs: TriageMaintainer attention needed!
    on Jun 6, 2021
  2. andyleejordan commented on Jun 14, 2021

    @andyleejordan
    Member

    This is interesting but a bit obscure to get fixed for the time being. I've marked it as up-for-grabs in case someone else wants to take a look.

  3. BrianL-STCU commented on Mar 19, 2026

    @BrianL-STCU

    Just noticed this problem in PowerShell 7.6. This is kind of a kludgy workaround, it would be nice to fix.

  4. BrianL-STCU commented on Mar 19, 2026

    @BrianL-STCU

    The workaround as written above doesn't work for me. I get "InvalidOperation: Unable to find type [Microsoft.PowerShell.EditorServices.Extensions.FileRange]."

    I can use Add-Type to recognize the type, but this also feels fragile, and there is still a problem:

    Add-Type -AssemblyName $psEditor.GetType().Assembly.Location
    $Context = $psEditor.GetEditorContext()
    $Range = [Microsoft.PowerShell.EditorServices.Extensions.FileRange]::new($context.SelectedRange.Start,$context.SelectedRange.End)
    MethodException: Cannot find an overload for "new" and the argument count: "2".
    
  5. added a commit that references this issue on Jun 18, 2026
    37dc288
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions