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?
You can enable the predictive IntelliSense feature with the option cmdlet for PSReadline. To disable the feature, set the source back to none.
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.
Use Visual Studio Code instead of PowerShell ISE.
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.
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With