# Running an executable on a remote computer using Invoke-Command

**URL:** <https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921>\
**Category:** PowerShell Help\
**Created:** [January 17, 2019, 4:34pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921 "2019-01-17T16:34:34Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 17, 2019, 4:34pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/1 "2019-01-17T16:34:34Z")

</div>

I am a newbie to Powershell.

I have been trying to run an executable using the invoke-command using ScriptBlock and trying it with just -FilePath. I get the file copied to the remote computer and directory but then I get errors. I am trying to use switches with the executable /install /quiet /norestart.

I would love some help to figure out where I am going wrong.

This is my Script:  
\<p class=“p1”\>#Variables\</p\>  
\<p class=“p2”\>$computername = Get-Content servers.txt\</p\>  
\<p class=“p3”\>$sourcefile = “c:\shared\install.exe”\</p\>  
\<p class=“p1”\>#This section will install the software\</p\>  
\<p class=“p2”\>foreach ($computer in $computername) \</p\>  
\<p class=“p4”\>{\</p\>  
\<p class=“p2”\>$destinationFolder = “\$computer\C$\Temp”\</p\>  
\<p class=“p1”\>#It will copy $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.\</p\>  
\<p class=“p2”\>if (!(Test-Path -path $destinationFolder))\</p\>  
\<p class=“p4”\>{\</p\>  
\<p class=“p2”\>New-Item $destinationFolder -Type Directory\</p\>  
\<p class=“p4”\>}\</p\>  
\<p class=“p2”\>Copy-Item -Path $sourcefile -Destination $destinationFolder\</p\>  
\<p class=“p6”\>Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process "c:\temp\Install.exe /install /quiet /norestart "}\</p\>  
Here is the error message. I also confirmed that the file did get copied to the remote server.  
\<p class=“p1”\>PS C:\Users\poponova\> C:\Shared\invoke3.ps1\</p\>  
\<p class=“p2”\>This command cannot be run due to the error: The system cannot find the file specified.\</p\>  
\<p class=“p2”\>+ CategoryInfo: InvalidOperation: (🙂 [Start-Process], InvalidOperationException\</p\>  
\<p class=“p2”\>+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand\</p\>  
\<p class=“p2”\>+ PSComputerName: server1\</p\>  
On the remote server the file is in the directory but it is not installed. I appreciate any help.

---

<div class="post-metadata">

**Author:** ![terry-mckenna](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@terry-mckenna](https://forums.powershell.org/u/terry-mckenna)\
**Post date:** [January 17, 2019, 5:09pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/2 "2019-01-17T17:09:39Z")

</div>

If you run: Start-Process "c:\temp\Install.exe /install /quiet /norestart " on the local machine, does it install?

Try: Invoke-Command -ComputerName $computer -ScriptBlock {Invoke-Command {‘c:\temp\Install.exe /install /quiet /norestart’}}

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [January 17, 2019, 6:28pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/3 "2019-01-17T18:28:58Z")

</div>

I think you are receiving the error because PowerShell thinks the following is a full program name or path.

"c:\temp\Install.exe /install /quiet /norestart "

Try this.

```
Invoke-Command-ComputerName$computer-ScriptBlock {Start-Process-FilePath 'c:\temp\Install.exe' -ArgumentList '/install /quiet /norestart'}
```

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 18, 2019, 8:37am UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/4 "2019-01-18T08:37:21Z")

</div>

Yes, running Start-Process and the command locally on the remote computer does work.

When I ran your script, this is the output shown, no error, however, it did not run. The file did copy to the remote directory.  
\<p class=“p2”\>PS C:\Users\poponova\> C:\Shared\invoke3.ps1\</p\>  
\<p class=“p2”\>c:\temp/install.exe /install /quiet /norestart\</p\>

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 18, 2019, 8:44am UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/5 "2019-01-18T08:44:19Z")

</div>

This did not work either. The following is the error.

&nbsp;  
\<p class=“p1”\>PS C:\Users\poponova\> C:\Shared\invoke3.ps1\</p\>  
\<p class=“p2”\>This command cannot be run due to the error: The system cannot find the file specified.\</p\>  
\<p class=“p2”\>+ CategoryInfo: InvalidOperation: (🙂 [Start-Process], InvalidOperationException\</p\>  
\<p class=“p2”\>+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand\</p\>  
\<p class=“p2”\>+ PSComputerName: server1\</p\>

---

<div class="post-metadata">

**Author:** ![turboshell](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@turboshell](https://forums.powershell.org/u/turboshell)\
**Post date:** [January 18, 2019, 9:14am UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/6 "2019-01-18T09:14:59Z")

</div>

Hi,

Try this, and tell us if this will work for you 🙂

Put that code inside script block of invoke-command

([WMICLASS]"\\localhost\ROOT\CIMV2:win32\_process").Create("C:\app.exe /arg1 /arg2...")

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 18, 2019, 10:39am UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/7 "2019-01-18T10:39:26Z")

</div>

thank you. This was returned. The file copied, but did not run

PSComputerName : server1  
RunspaceId : 30e799f6-08fc-48b6-973d-97cc304fa453  
\_\_GENUS : 2  
\_\_CLASS : \_\_PARAMETERS  
\_\_SUPERCLASS :  
\_\_DYNASTY : \_\_PARAMETERS  
\_\_RELPATH :  
\_\_PROPERTY\_COUNT : 2  
\_\_DERIVATION : {}  
\_\_SERVER :  
\_\_NAMESPACE :  
\_\_PATH :  
ProcessId : 4964  
ReturnValue : 0

---

<div class="post-metadata">

**Author:** ![fullenw1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fullenw1/32/171_2.png) [@fullenw1](https://forums.powershell.org/u/fullenw1)\
**Post date:** [January 18, 2019, 2:40pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/8 "2019-01-18T14:40:25Z")

</div>

What do you get with this?

```
Invoke-Command -ComputerName $Computer -ScriptBlock {Get-Item -Path 'C:\temp\Install.exe'}
```

---

<div class="post-metadata">

**Author:** ![neild](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@neild](https://forums.powershell.org/u/neild)\
**Post date:** [January 18, 2019, 2:48pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/9 "2019-01-18T14:48:31Z")

</div>

When using Invoke-Command on remote machines aren’t credentials needed?

```
Invoke-Command -ComputerName server01 -Credential domain01\user01 -ScriptBlock {Get-Culture}
```

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 18, 2019, 6:25pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/10 "2019-01-18T18:25:11Z")

</div>

I am still getting error with that one as well. I am sure it is a double hop error. I just can’t find my way around the problem.

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 18, 2019, 6:29pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/11 "2019-01-18T18:29:33Z")

</div>

I get prompted for my credentials but get an Access Denied error.

PS C:\Windows\system32\> Invoke-Command -ComputerName Server01 -Credential Domain\user -ScriptBlock {Get-Culture}  
[PHLWQ40A] Connecting to remote server server01 failed with the following error message : Access is denied. For more  
information, see the about\_Remote\_Troubleshooting Help topic.  
+ CategoryInfo : OpenError: (server01:String) , PSRemotingTransportException  
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken

---

<div class="post-metadata">

**Author:** ![rburrs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rburrs/32/2009_2.png) [@rburrs](https://forums.powershell.org/u/rburrs)\
**Post date:** [January 18, 2019, 7:46pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/12 "2019-01-18T19:46:14Z")

</div>

Try the solution provided here: [https://powershell.org/forums/topic/need-to-run-exe-with-parameters-on-multiple-remote-machines/](https://powershell.org/forums/topic/need-to-run-exe-with-parameters-on-multiple-remote-machines/)

&nbsp;

---

<div class="post-metadata">

**Author:** ![fullenw1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fullenw1/32/171_2.png) [@fullenw1](https://forums.powershell.org/u/fullenw1)\
**Post date:** [January 19, 2019, 1:10pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/13 "2019-01-19T13:10:48Z")

</div>

> [@](#):
>
> [PHLWQ40A] Connecting to remote server server01 failed with the following error message: Access is denied.

This looks more like a remoting problem…

Please try this first to confirm remoting is working fine:

```
Enter-PSSession server01
```

If this works try interactively in this remote session

```
c:\temp\Install.exe /install /quiet /norestart
```

---

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [January 20, 2019, 11:23pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/14 "2019-01-20T23:23:18Z")

</div>

You’ve confirmed the install.exe copied over and is located in the C:\Temp folder on the remote machine?  
If so, then when you run the Start-Process in the Invoke-Command you probably need to use the -Wait parameter. It’s likely the install.exe starts but is never allowed to finish because the session is closed right after the scriptblock runs.

Try this:

```
Invoke-Command -ComputerName $computer -ScriptBlock {
    Start-Process 'c:\temp\Install.exe' -ArgumentList '/install /quiet /norestart' -Wait
}
```

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 21, 2019, 10:26am UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/15 "2019-01-21T10:26:50Z")

</div>

I could have sworn at one point I tried the -wait. I probably use the wrong parameter. It worked. Thank you so much! I spent the whole day Friday on this and should have waked away sooner so I could view with fresh eyes.

---

<div class="post-metadata">

**Author:** ![poponova](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@poponova](https://forums.powershell.org/u/poponova)\
**Post date:** [January 24, 2019, 1:44pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/16 "2019-01-24T13:44:10Z")

</div>

Ok, so now I am having a different issue. the command runs one at a time on each server in the server.text file. The executable that I am running can take hours to complete so I want them to run concurrently on each server in the file. I have tried commands I have found to run parallel unsuccessfully. Any suggestions? This is my script that works well one at a time.

#Variables  
$computername = Get-Content c:\shared\servers.txt  
$sourcefile = “c:\shared\dummy.exe”  
#This section will install the software  
foreach ($computer in $computername)  
{  
$destinationFolder = “\$computer\C$\install”  
#It will copy $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.

```
if (!(Test-Path -path $destinationFolder))
{
    New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder

```

Invoke-Command -ComputerName $computer -ScriptBlock {  
Start-Process ‘c:\install\dummy.exe’ -Wait  
}

---

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [January 24, 2019, 2:05pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/17 "2019-01-24T14:05:27Z")

</div>

Invoke-Command has the -AsJob parameter you could try or wrap your code in the scriptblock of Start-Job.

---

<div class="post-metadata">

**Author:** ![fullenw1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fullenw1/32/171_2.png) [@fullenw1](https://forums.powershell.org/u/fullenw1)\
**Post date:** [January 25, 2019, 2:21pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/18 "2019-01-25T14:21:39Z")

</div>

I agree with John.

Just to illustrate:

```
Invoke-Command -ComputerName $computer -ScriptBlock {
Start-Process 'c:\install\dummy.exe' -Wait
} -AsJob
```

Then you can check the status of all jobs with:

```
Get-Job
```

As soon as all jobs are finished, retrieve the result with:

```
Get-Job | Receive-Job -Keep
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/running-an-executable-on-a-remote-computer-using-invoke-command/11921/19 "2024-05-16T20:33:24Z")

</div>


