Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellisense doesn't work after piping from custom cmdlet (Powershell ISE)

Tags:

powershell

If I type the following line in the PowerShell ISE editor, I get Intellisense after the dot operator in $_ variable:

Get-ChildItem ATextFile.txt | foreach { $_.FullName }

In this case, $_ is an instance of System.IO.FileSystemInfo. The editor will properly list all accessible members from this object.

Now, if I write:

function GetFile {
  return [System.IO.FileInfo]::new(".\ATextFile.txt")
}

GetFile | foreach { $_.FullName }

The script runs fine, but Intellisense doesn't work after the dot operator in $_.

Am I missing a syntax to make IntelliSense work correctly? Maybe an annotation to "document" the returning value?

like image 280
Christiano Kiss Avatar asked May 22 '17 13:05

Christiano Kiss


People also ask

How do I enable IntelliSense in PowerShell?

You can enable the predictive IntelliSense feature with the option cmdlet for PSReadline. To disable the feature, set the source back to none.

What is Microsoft's IntelliSense and how does it benefit a PowerShell ISE user?

IntelliSense is an automatic-completion assistance feature that is part of Windows PowerShell ISE. IntelliSense displays clickable menus of potentially matching cmdlets, parameters, parameter values, files, or folders as you type.

What can I use instead of PowerShell ISE?

Use Visual Studio Code instead of PowerShell ISE.

What type of line should you include to ensure that the script is run under the correct shell?

A shell script is a text file containing one or more commands. The first line contains a shebang #! followed by the path to the shell, in this case bash - this acts as an interpreter directive and ensures that the script is executed under the correct shell.


1 Answers

You are looking for the OutputType attribute above the Param section:

function GetFile {
    [OutputType([System.IO.FileInfo])]
    Param(

    )

  return [System.IO.FileInfo]::new(".\ATextFile.txt")
}

Please consider to rename your file to reflect approved verbs e. g. Get-File. Also note that the return statement is not necessary in PowerShell, so your function should look like this:

function Get-File 
{
    [OutputType([System.IO.FileInfo])]
    Param
    (
    )

    [System.IO.FileInfo]::new(".\ATextFile.txt")
}
like image 63
Martin Brandl Avatar answered Oct 13 '22 09:10

Martin Brandl