business.com receives compensation from some of the companies listed on this page. Advertising Disclosure

Home

How to Remotely Invoke Applications with PowerShell

Adam Bertram

PowerShell offers several ways to invoke applications on remote computers.

Not all applications are created with remote execution in mind. PowerShell provides several ways to invoke applications on remote computers.

Have you ever had an application handed to you and was told to run it on a bunch of computers? Come to find out that the app was never built that way — it was designed to only be executed locally. This might have stumped you for awhile until you realized you could run the app with various deployment tools. These tools would copy the app to your computers and execute it. However, what if that was overkill? Maybe you just want a quick way to copy the app to a few machines and get it ran. This is where PowerShell comes in handy.

PowerShell offers a few different ways to execute applications on remote computers. In this tutorial we'll show you three different ways — two use WMI and the third preferred way uses PowerShell remoting.

1. Create()

One way to run a process on a remote computer is to use the Win32_Process WMI class. Win32_Process is a WMI class that has a static method called Create(). This Create() method allows you to pass a EXE to it, which runs that EXE on the remote computer. In PowerShell, there are a couple of ways to make this happen.

The first way is through the WMICLASS type accelerator. WMICLASS is a shortcut to enable access to all of the classes static properties and methods. Since Create() is a static method we don't actually have to instantiate a Win32_Process object at all. We can simply call the method with the EXE as the first argument and it will run.

([WMICLASS]"MEMBERSRV1RootCIMV2:Win32_Process").create("notepad.exe")

In this instance, we're running the notepad.exe process on the computer MEMBERSRV1. However, knowing that this process is not interactive means you won't actually see notepad.exe pop up on a logged in console. Remote process execution is best left to applications that don't require interactive input anyway.

2. Invoke-WmiMethod

Next, PowerShell has the Invoke-WmiMethod cmdlet. We can again call the Create() method, but this time the method is not quite so convoluted. Using Invoke-WmiMethod is a more user-friendly way to call static methods like Create()

Invoke-WmiMethod –ComputerName MEMBERSRV1 -Class win32_process -Name create -ArgumentList "notepad"

Here we're accomplishing the same exact task as before. This time we're just expressing the Win32_Process class, and the parameter to Create(); the EXE itself, a little differently.

3. PowerShell remoting

Finally, we have PowerShell remoting. The previous two methods that used WMI depended on remote DCOM being enabled on the computer. This may or may not be a problem but can sometimes pose a security risk. You can also use PowerShell remoting through the Invoke-Command cmdlet to kick off a process on a remote computer as well as through WSMAN, which is a newer, more secure protocol.

To do this, we'll use a combination of two cmdlets: Invoke-Command to give us the ability to run a command on the remote computer and Start-Process to actually execute the process.

Invoke-Command –ComputerName MEMBERSRV1 –ScriptBlock {Start-Process notepad.exe}

Invoke-Command is a PowerShell cmdlet that allows us to execute code on a remote computer just as if it were local. It has a ScriptBlock parameter that we can insert any kind of code to run locally on that remote computer. In this instance, we're using Start-Process, which runs a specific application.

You can see that there are numerous ways to remotely invoke processes on computers with PowerShell. Start with Invoke-Command/Start-Process to see if that method gives you the results you're looking for. If not, you might need to look into the older methods of using WMI. At least one of these methods will get that process running remotely!

Image Credit: kikujungboy/Shutterstock
Adam Bertram
business.com Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.