How to disable/override PowerShell dot notation

Viewed 166

Commands in PowerShell are almost like Bash, but the dot notation expansion is creating a lot of work for me. Currently I have to wrap a lot of command parameters in quotes:

.\mvnw.cmd -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar

becomes

.\mvnw.cmd -D"maven.repo.local"=".m2/repository" deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId="com.zippo" -DartifactId=test -Dversion="1.0" -Dpackaging=jar -Dfile="test-1.0.jar"

How can I disable dot notation, or override the dot operator, replace it with something else, etc.?

1 Answers

How can I disable dot notation?

  • The problem isn't dot notation, it is a bug in how PowerShell parses arguments to be passed to external programs: a token such as -foo.bar is broken into two arguments, -foo and .bar - see GitHub issue #6291 (accidental duplicate: GitHub issue #15541); this is but one among several related bugs, present up to at least PowerShell 7.2.6.

  • Quoting is indeed needed to work around this problem, but you can simplify it by enclosing each affected argument as a whole in '...' (or "...", if you need string interpolation):

    .\mvnw.cmd '-Dmaven.repo.local=.m2/repository' 'deploy:deploy-file' -'Durl=http://zippo:8081/repository/grinch/' '-DrepositoryId=nexus' -'DgroupId=com.zippo' '-DartifactId=test' '-Dversion=1.0' '-Dpackaging=jar' '-Dfile=test-1.0.jar'
    

Alternatives:

  • You can use --%, the stop-parsing token, which obviates the need for quoting, but note its many limitations, notably the inability to (directly) incorporate PowerShell variables and expressions into the command - see this answer for details.

    # --% passes the remainder of the command line as-is to the target program
    .\mvnw.cmd --% -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar
    
  • You can call via cmd /c, in which case you can quote your entire command line, which avoids the parsing bug; note that any output relayed by cmd.exe is subject to decoding based on the character encoding stored in [Console]::OutputEncoding, so unless mvnw uses this encoding for its output, you may have to (temporarily) set [Console]::OutputEncoding to the actual encoding first.

    cmd /c '.\mvnw.cmd --% -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar'
    
Related