PowerShell: Ctrl+space won't work in any code declared within a function

Markus Johnsson 21 Reputation points
2022-09-26T08:38:16.58+00:00

Hello, friends!

This is my first post on this forum and I am fairly new to PowerShell and have been experimenting with it on and off in the past and I have recently started doing a bit of scripting so my knowledge isn't that solid yet so, please, bare with me. (However, please know that I've read all relevant documentation I could find without finding any answers to my little problem.)

My problem is very trivial but oh so disturbing: I like the PowerShell feature Ctrl+Space and when I write scripts with parameters but where the code is not declared within a function, Ctrl+Space works just fine. However, when I wrap the code in a function body then simply nothing at all happens when I press Ctrl+Space. No parameter options show up any longer. I mean surely this should work in functions?

I take a most minimal example that works with Ctrl+Space just to make clear what works and not, while sticking to the simplest possible (almost):

param (  
    [Parameter()]  
    [string]  
    $Var = "Test"  
)  
    $ParamValue = $Var  
    $ParamValue  
  

But as soon as this snippet is wrapped in a function...

Function FunctionName {  
    param (  
        [Parameter()]  
        [string]  
        $Var = "Test"  
    )  
        $ParamValue = $Var  
        $ParamValue  
}  
FunctionName  

...Ctrl+Space will simply not show any parameters. Also, this means that my parameter variables are NOT recognized either so I can't complete or cycle through mine and the systems parameter names with TAB either, which is a horrible ordeal! :D

On Windows 10 and Server 2019 I am working with PowerShell Version 7.2.6 as my standard engine but I also use the latest Preview build and Windows PowerShell 5.1 for testing purposes. I have the same issues in all of these environments when it comes to this issue.

Have any of you experienced this behavior as well and do you know what I'm doing wrong?

I understand that this is not a significant problem but I felt it is still a feature that simply should work - no matter who wrote the script - since the feature is there for a reason, right? And it should work in any case.

I'm prepared for that, whatever the cause for this issue may be, it is most certainly located on my side but I'm at a total loss as to what it may be, but I would really like to know why this won't work and I would really appreciate any kind of helping hands here: links to or names of documents that will explain why it won't work in functions (which is extremely disturbing) or that will treat the subject so I can get some sleep tonight ; )

(note: I'm couldn't provide the tags I wanted because I was forced to choose among existing ones but sure this question must be valid even here?)

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,195 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,906 Reputation points
    2022-10-04T18:56:14.16+00:00

    Well, yes, the Ctrl+Space works with a script that contains no functions. But consider this: if the SCRIPT file contains a function, the FUNCTIONS parameters aren't exposed until the function is loaded into the session. Thus, the need to use dot-sourcing unless it's you intention to use the function in the current PowerShell session.

    But if your intention is to run a script and not pollute your PowerShell sessions memory with functions that make no sense to retain (e.g., they specific the script in which they reside and are of no use outside the script), then you don't dot-source the script.

    #This using the file Get-TestBBB which contains a function  
    #  
    #  
    # When run as a script (without dot-sourcing), the parameter is passed,  
    # but within the script the function Get-TestBBB is invoked with no parameter.  
    # The result? The default value of the Test parameter (an empty string)  
    # is used.  
    PS C:\junk> .\get-testbbb.ps1 -Test "Blah"  
    It doesn't work  
    PS C:\junk>  
      
    # The same thing happens because the function is invoked with no value  
    # from within the ps1 file.  
    PS C:\junk> . .\get-testbbb.ps1 -Test "Blah"  
    It doesn't work  
      
    # But NOW, becasue the script was dot-sourced, the funtion Get-TestBBB  
    # is preserved in this session's "memory"  
      
    PS C:\junk> gci function:get-*  
      
    CommandType     Name                                               Version    Source  
    -----------     ----                                               -------    ------  
    Function        Get-Verb  
    Function        Get-FileHash                                       3.1.0.0    Microsoft.PowerShell.Utility  
    Function        Get-TestBBB  
      
    # NOW, we only have to invoke the function. The function invokation WITHIN  
    # the script isn't part of the function.  
      
    PS C:\junk> get-testbbb -test "It Works!"  
    It Works!  
      
    # And, so does Ctrl+Space  
    PS C:\junk> get-testbbb -Test  
    Test                 Debug                WarningAction        ErrorVariable        InformationVariable  OutBuffer  
    Verbose              ErrorAction          InformationAction    WarningVariable      OutVariable          PipelineVariable  
      
    [string] Test  
    

11 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2022-09-30T19:39:28.64+00:00

    It looks like a difference (or a bug -- I don't know if I'd call it a feature!) between PowerShell 5.1 and 7.2 and what combination of parameter declarations are acceptable

    You've declared the parameter $ComputerName with a default value and, at the same time declared that the parameter is mandatory. That's a contradiction. Change the "$true" to "$false" and you now have an optional parameter with a default value. If you want to verify that the parameter hasn't been invoked without being provided with a value (or a null value) you can add [ValidateNotNullOrEmpty() between lines 4 and 5 in the function.

    Now your function loads properly and PS7's Ctrl+Space works as advertised.


  2. Markus Johnsson 21 Reputation points
    2022-10-01T06:24:01.123+00:00

    I have noticed a small progress here that I wanted to show:

    I was messing with this [ValidateNotNullOrEmpty()]-thing when I discovered something! I entered a simple skeleton function directly in the prompt of either PowerShell 5.1 or 7.2.6 and CTRL + SPACE worked! And parameter value was grabbed perfectly!

    This is the little snippet I used:

    Function Test-Me {  
        [cmdletbinding()]  
        Param(  
            [ValidateNotNullOrEmpty()]   
            [string[]]$Item  
        )  
        Process {  
            $Item  
        }  
    }  
    

    if I call the function using Test-Me -Item Test it prints test and if I supply $null it will throw an error as expected. But as soon as I write this function in VS Code or Notepad and try to invoke it it by calling it with .\Test-Me or .\Test-Me -Item $null it will not work. This must be some kind of progress, right?

    Maybe this will help you understand why it fails with my editors and script-files but works directly in the prompt?


  3. Markus Johnsson 21 Reputation points
    2022-10-02T10:00:50.38+00:00

    ( EDIT: I tried to post via "reply" but it didn't work for hours and still doesn't for me )

    Thank you for your explanation. Much appreciated.

    However, I tried dot-sourcing and it didn't work either :( It is the exact same result - no list from Ctrl + Space and no parameters that will work as long as a function is defined.

    When you use VS Code, are you using PWSH or PowerShell in the "Terminal"?

    I use PWSH and the PowerShell Extension terminal. Both act the same when it comes to my problem.

    Keep in mind that you're running PWSH or PowerShell from a different host (VS Code) and, if I remember correctly, VS Code is using some sort of terminal emulation that may have a different use for the Ctrl-Space.

    I don't know anything about the terminal emulation but what I do know is that the VS Code terminals act exactly the same as the standard "console hosts" (the windows standard PowerShell console window) with PWSH or PowerShell. Ctrl + Space works perfectly in VS Code too when the code isn't in a function. I also have a second computer where I have no profile on my PWSH and PowerShell hosts and the problem is exactly the same on that computer.

    I'm assuming you meant that you're using Notepad*++* and not Microsoft's "Notepad". :-)

    Hehe, your assumption was wrong. I meant Microsoft Notepad. That boring old junkpad. But I don't normally use it for scripting. I have been using it while debugging this issue of mine with some simple code I switch back and forth between the hosts and editors just to see that there isn't anything bound to the editor in itself. But I could never actually code in Notepad. Too tedious and too easy to make typos. : )

    0 comments No comments

  4. Markus Johnsson 21 Reputation points
    2022-10-02T10:09:24.067+00:00

    (For some reason, the forum doesn't allow me to directly reply to your reply any longer so I wrote here)

    Thank you for your explanation. Much appreciated.

    However, I tried dot-sourcing and it didn't work either :( It is the exact same result - no list from Ctrl + Space and no parameters that will work as long as a function is defined.

    When you use VS Code, are you using PWSH or PowerShell in the "Terminal"?

    In VC Code, I use PWSH and the PowerShell Extension terminal. Both terminals act the same as the external shells do when it comes to my problem.

    Keep in mind that you're running PWSH or PowerShell from a different host (VS Code) and, if I remember correctly, VS Code is using some sort of terminal emulation that may have a different use for the Ctrl-Space.

    I don't know anything about the terminal emulation but what I do know is that the VS Code terminals act exactly the same as the standard "console hosts" (the windows standard PowerShell console window) with PWSH or PowerShell. Ctrl + Space works perfectly in VS Code too when the code isn't in a function. I also have a second computer where I have no profile on my PWSH and PowerShell hosts and the problem is exactly the same on that computer.

    I'm assuming you meant that you're using Notepad*++* and not Microsoft's "Notepad". :-)

    Hehe, your assumption was wrong. I meant Microsoft Notepad. That boring old junkpad. But I don't normally use it for scripting. I have been using it while debugging this issue of mine with some simple code I switch back and forth between the hosts and editors just to see that there isn't anything bound to the editor in itself. But I could never actually code in Notepad. Too tedious and too easy to make typos. : )

    0 comments No comments