Command switches interpreted as positioned parameter


I have created the following psm1 script to have some standard custom functions easy at hand.

function Connect-4twRemoteServer {
    param (
        # Enter the server you want to connect to
        # Please specify credentials for the connection
        $4twCreds = [System.Management.Automation.PSCredential]::Empty,
        # Parameter help description
    begin {
        Write-Debug "Trying to connect to $4twServer"
        Write-Debug "With user $($4twCreds.UserName)"
        Write-Debug "In Begin section"
        if ($4twServer -eq "") {
            Write-Warning "You must specify a server to connect to."
        if ($GetCreds){
            $4twCreds = Get-Credential

gives this debug output:

PS C:\Users\l> Connect-4twRemoteServer -4twServer "" -Debug

PowerShell credential request
Enter your credentials.
Password for user *

DEBUG: Trying to connect to -4twServer
DEBUG: With user
DEBUG: In Begin section

So the -4twServer (Which I didn’t type myself, but tap-completed) is suddenly treated as positional parameter 0 and the value it should have is parameter 1.

Please advice, what is wrong?

[EDIT] I have tested with Code and PS 5.1/7.1 and in a normal PS 7.1 console [/EDIT]

Regards Lars Mortensen

I’m having trouble finding any official documentation to point you towards, but I am fairly certain that parameter names should not (cannot?) start with a number.


I would have sworn I have tried that, but just tried again, and now it works.
Thank you!