If you are in a domain environment, you can also use:
winrs -r:PCNAME cmd
This will open a remote command shell.
Answer from user3744855 on Stack OverflowI think you're looking for PsExec
This is what you'd do to execute a CLI program on a remote computer using PsExec:
psexec \\REMOTECOMPUTER "path_to_program_executable"
You can learn more about PsExec and its various options along with some examples here
On more option is the built in winrs
winrs.exe -r:<RemoteComputerName> <TheCommandToRun>
winrs.exe -r:MyRemoteServer ipconfig
winrs vs. psexec
advantages winrs
- Built in, no download needed
- Faster and more reliable than psexec
advantages psexec
- Works with IP (winrs needs the computername (NetBIOS name) of the machine)
- Can run processes in system-context
powershell 2.0 - How to execute a CMD file in remote computer - Stack Overflow
TLDR of how to run commands on a remote Windows system? (and assorted questions)
How do you remotely use command prompt on another computer
Running a .cmd file on a remote machine
Videos
My experience in automation is limited to Linux (ssh, scp for file transfers). I'd like to a convenient way to run scripts/commands on a remote Windows PC, as well as transfer files, and it seems Powershell is the tool for that. I could install an SSH server, but if Powershell can achieve the same convenience without spending too much time learning, I'd prefer that, since I'd be able to use it on other Windows PCs without installing new software on them. My example scenario is:
-
MYDESKTOP: Windows 10 PC which has 3 files: data.csv, localscript.ps1, remotescript.ps1, IP 192.168.1.10.
-
MYTARGET: Windows 10 PC with local account, username alice, password mypassword, IP 192.168.1.25
I would like to:
-
upload data.csv to MYTARGET's c:/data
-
execute remotescript.ps1 locally on MYDESKTOP. This script contains a mixture of local commands to be executed on MYDESKTOP and remote commands to be executed on MYTARGET (eg Get-Process should return the processlist of MYDESKTOP, while a different Get-Process returns the process list of MYTARGET)
-
execute localscript.ps1 entirely on MYTARGET. This script iterates over the local filesystem and does stuff, so not appropriate for being executed remotely if every iteration of a loop results in a network call.
...
$ $session = New-PSSession -ComputerName 192.168.1.25 New-PSSession : [192.168.1.25] Connecting to remote server 192.168.1.25 failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic. At line:1 char:12 + $session = New-PSSession –ComputerName 192.168.1.25 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException + FullyQualifiedErrorId : CannotUseIPAddress,PSSessionOpenFailed`
So already it's not as convenient as SSH, I gotta configure my PC to allow this outbound connection, and I guess I will also have to configure the remote PC to allow inbound connections.
Can someone give me the TLDR of how I should be doing this? I can start reading how to to allow IP connection and find the setting, and also allow username/password authentication, but should I even do this? If I open up the configuration and some malicious PC on the network starts to brute-force IP/password auth, will Windows automatically throttle/ban them?
This is gonna sound bad, but I'm not interested in learning more about Windows than the minimum I need to to achieve this goal, it doesn't really benefit me in the long term compared to the huge pile of things already on my to-learn list.
What if I want to run defrag or chkdsk on a client computer. Would cmd be a good option?
PSExec \$computer cmd /c defrag.exe
PSExec \$computer cmd /c chkdsk.exe
Sky’s the limit. But most of my computers get reformatted before defrag is necessary. Also, don’t forget that you shouldn’t even try defragging SSDs.
How do you remotely use command prompt on another computer
@Microsoft
I would like a PowerShell script to run on my local machine and launch a .cmd file on a remote server.
The .cmd file references f:\ and won’t run if it doesn’t have f: mapped how it expects.
I can’t edit the script that the .cmd file uses as it’s not mine I just perform some routine steps on the server that I would like to automate for myself.
I have been playing with invoke-command without success. Another issue is that the machine seems to have an old version of PowerShell, I’ll add what version it’s on when I’m back in the office.
