# Invoke-Command to run a Powershell script on a remote computer

**URL:** <https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268>\
**Category:** PowerShell Help\
**Created:** [April 16, 2021, 8:50am UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268 "2021-04-16T08:50:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbell](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jbell](https://forums.powershell.org/u/jbell)\
**Post date:** [April 16, 2021, 8:50am UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268/1 "2021-04-16T08:50:47Z")

</div>

Hello,

I am trying to automate a process to uninstall an application on several servers. I have tried running the following commands remotely and they do not seem to work, however, when running locally it seems to run OK and I am not sure why it won’t work. PSRemoting is enabled.

```auto
$serverName = Server1, Server2, Server3

Invoke-Command -ComputerName $serverName -ScriptBlock {
    Start-Process c:\windows\temp\uninstallApp.ps1

```

The Powershell command it is attempting to run does the following

```auto
cd 'C:\Program Files\App\Agent\x86\'
.\FrmInst.exe /forceuninstall /silent

```

Any ideas where I might be going wrong?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 16, 2021, 9:07am UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268/2 "2021-04-16T09:07:08Z")

</div>

I’d recommend to use [Invoke-Command](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.1) with the parameter `-FilePath` instead of `-ScriptBlock`. This way you can provide a local PowerShell script to run it on remote computers.  
Please read the help completely including the examples to learn how to use it.

---

<div class="post-metadata">

**Author:** ![jbell](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jbell](https://forums.powershell.org/u/jbell)\
**Post date:** [April 16, 2021, 9:44am UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268/3 "2021-04-16T09:44:36Z")

</div>

Hi Olaf,

Thank you very much for this, managed to change my code entirely and it is working a treat. To help others if they experience the same issue I thought I would show my code.

```auto
#Server names
$serverName = "Server1"

#Establish Session
$session = New-PSSession -ComputerName $serverName

#Uninstall McAfee
Invoke-Command -Session $session -FilePath "M:\software\uninstallApp1.ps1"

#Install SentinelOne
Invoke-Command -Session $session -FilePath "M:\software\installApp2.ps1"

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 16, 2021, 9:47am UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268/4 "2021-04-16T09:47:43Z")

</div>

I’m glad it helped. 🙂  
Thanks for sharing. 👍🏽

---

<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:26pm UTC](https://forums.powershell.org/t/invoke-command-to-run-a-powershell-script-on-a-remote-computer/16268/5 "2024-05-16T20:26:43Z")

</div>


