How can I execute a program on a remote machine using powershell?
6 Answers
The cool new way to do this is with WinRM. I've seen this demo'd on Windows Server 2008 R2, although there is a download with powershell v2 & WinRM for other windows operating systems.
The not so cool (or new) way to do this is to use psexec, which isn't powershell, but I'm sure there's some way to invoke it via powershell-esque syntax.

- 10,796
- 7
- 37
- 47
-
1Just to add a little more detail, for a quick test setup, do a "winrm quickconfig" on the server to set it up to receive requests. Use the Invoke-Command cmdlet from the client to send a command to the server. – Ameer Deen May 23 '11 at 14:29
-
Thank you for this answer, it was helpful to me, after configuring winrm properly I was able to remotely execute a bat file, I have supplied my code in my answer to this question. – SSH This Apr 15 '13 at 17:34
You could also use WMI and remotely start a process. It won't be interactive and you'll have to trust that it will end on its own. This doesn't require anything on the remote computer other than open ports for WMI.
Function New-RemoteProcess {
Param([string]$computername=$env:computername,
[string]$cmd=$(Throw "You must enter the full path to the command which will create the process.")
)
$ErrorActionPreference="SilentlyContinue"
Trap {
Write-Warning "There was an error connecting to the remote computer or creating the process"
Continue
}
Write-Host "Connecting to $computername" -ForegroundColor CYAN
Write-Host "Process to create is $cmd" -ForegroundColor CYAN
[wmiclass]$wmi="\\$computername\root\cimv2:win32_process"
#bail out if the object didn't get created
if (!$wmi) {return}
$remote=$wmi.Create($cmd)
if ($remote.returnvalue -eq 0) {
Write-Host "Successfully launched $cmd on $computername with a process id of" $remote.processid -ForegroundColor GREEN
}
else {
Write-Host "Failed to launch $cmd on $computername. ReturnValue is" $remote.ReturnValue -ForegroundColor RED
}
}
Sample usage:
New-RemoteProcess -comp "puck" -cmd "c:\windows\notepad.exe"

- 162
- 1
- 9

- 129
- 3
-
I'm getting errors on this and I can't say what it is happening. Is there any way to print the error? – Gabriel Guimarães Oct 26 '10 at 14:48
-
Thanks. This worked for me on a Windows 7 machine. I didn't try running it against a server (yet) but can't see why it wouldn't work (unless IT blocks WMI) – bmg002 May 23 '17 at 22:06
Interesting enough I used this to run notepad on a remote computer and it didn't appear. I checked the Task manager and the process ID that the call returned was indeed there!
Windows stated that this was a security concept and the process would run hidden/ or in the background!
This code helped me execute a bat file remotely, hopefully this helps someone in the future. You'll need to replace the creds and ComputerName vars at the top of this script.
$Username = "username"
$Password = "password"
$ComputerName = "remote.machine.hostname"
$Script = {C:\test.bat > C:\remotelog 2>&1}
#Create credential object
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
#Create session object with this
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
#Invoke-Command
$Job = Invoke-Command -Session $Session -Scriptblock $Script -AsJob
$Null = Wait-Job -Job $Job
#Close Session
Remove-PSSession -Session $Session

- 495
- 1
- 5
- 8
RE: Nick's answer
Yes, PowerShell v2 (with WinRM 2.0) is bundled with Server 2008 R2 and Windows 7. A downlevel version should be available soon for XP, Vista, 2003 and 2008.
You always have the option of using WMI also to run remote stuff, but it is must interact with the console, that won't be a viable method.

- 407
- 3
- 11