Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How can I execute a program on a remote machine using powershell?

share|improve this question

7 Answers 7

up vote 6 down vote accepted

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.

share|improve this answer
    
agreed - what he said. –  Trondh Oct 23 '09 at 12:08
1  
Just 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"
share|improve this answer
    
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

Here's the psexec/powershell link.

share|improve this answer

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!

share|improve this answer

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.

share|improve this answer

it all works really well - but ... I wanna start the application / program in another profile/user. Now it starts as in my credentials which I can't use. I want to start it in another users credentials.

I'm domain admin - and I don't have the password on the other user.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.