This post was most recently updated on December 7th, 2022.
2 min read.Heh, this article is going to be another one of those “Drat, I should’ve known this” articles. But maybe it’ll be helpful for someone else as well!
In this post I will explain how to get around the “Error: Invalid use of command line. Type “winrm -?” for help.” error being thrown at you. And if you’re like me (and your error is the same as mine), you’ll mostly get to blame yourself for this one!
Background
The other day I was tweaking some PowerShell Remoting settings.
winrm set winrm/config/client/auth @{CredSSP="true"}
But this kept throwing an error:
Error: Invalid use of command line. Type “winrm -?” for help.
Running winrm set -? produces this help:
Windows Remote Management Command Line Tool winrm set RESOURCE_URI [-SWITCH:VALUE [-SWITCH:VALUE] ...] [@{KEY="VALUE"[;KEY="VALUE"]}] [-file:VALUE] Modifies settings in RESOURCE_URI using specified switches and input of changed values via key-value pairs or updated object via an input file. Example: Modify a configuration property of WinRM: winrm set winrm/config @{MaxEnvelopeSizekb="100"} Example: Disable a listener on this machine: winrm set winrm/config/Listener?Address=*+Transport=HTTPS @{Enabled="false"} Example: Disable a certmapping entry on this machine: Winrm set winrm/config/service/certmapping?Issuer=1212131238d84023982e381f20391a2935301923+Subject=*.example.com+URI=wmicimv2/* @{Enabled="false"} See also: winrm help uris winrm help aliases winrm help input winrm help switches
But running winrm set winrm/config @{MaxEnvelopeSizekb=”100″} produces exactly the same error:
PS C:\Users\anko> winrm set winrm/config @{MaxEnvelopeSizekb="100"} Error: Invalid use of command line. Type "winrm -?" for help.
What’s up with that?
Reason
The help files have not been “localized” for PowerShell. In PowerShell, you need to enquote and escape more.
Let’s dive in, shall we?
Solution
Time needed: 5 minutes
Localizing the files for PowerShell.
- Enclose the configuration value itself in single quotes.
Something like this:
winrm set winrm/config/client/auth '@{CredSSP="true"}'
- Or, like in the misleading sample above but properly enclosed:
winrm set winrm/config '@{MaxEnvelopeSizekb="100"}'
- Likewise, in PowerShell, you must escape any variables in your script. A sample I found on GitHub (link in references) after digging into the error:
winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname="'"$ENV:ComputerName"'";CertificateThumbprint="'"$($Cert.Thumbprint)"'"}'
well, that’s it for today :)
References
- https://github.com/MicrosoftDocs/office-docs-powershell/issues/4884
- https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/winrmsecurity?view=powershell-7.3
- Don’t assign root domain to GitHub Pages if you use it for email! - January 14, 2025
- Experiences from migrating to Bitwarden - January 7, 2025
- 2024 Year Review – and 20 years in business! - December 31, 2024