How do I run one server command from CMD or PowerShell?
Place a remote command after the SSH destination to execute it and return to your local shell. Start with simple commands that do not require nested quoting. The remote account’s shell interprets the command on the server.
Instructions
1. Confirm the target with a read-only command
The example does not change server configuration.
ssh adminuser@203.0.113.10 hostname
$LASTEXITCODESSH prints the remote hostname, then PowerShell shows the native exit code. The same SSH line works in CMD.
2. Keep simple multiword commands together
PowerShell can quote a simple remote command as one string. Avoid nesting local and remote variable expressions unless you have checked the exact native-argument rules for your PowerShell version.
ssh adminuser@203.0.113.10 'uname -sr'This asks the remote Linux host for its kernel name and release. It uses no local variables or nested quotation marks.
3. Use an interactive session for complicated operations
Multiple parsing layers can turn a seemingly harmless string into a different command. For complex maintenance, open SSH normally and run clearly labelled remote commands one at a time. Avoid embedding passwords or untrusted input in the command string.
Check the result
- The hostname output identifies the expected remote machine.
- The exit code is captured immediately and matches the command outcome.
Troubleshooting
- A remote variable expands to a local value
- Review the local shell quoting rules or run the command interactively on the server.
- sudo asks for a terminal
- Use an approved interactive session or a purpose-designed least-privilege automation command rather than sending passwords through a pipe.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
