Icon Fame Journal.

Juicy entertainment chatter with tabloid flavor.

updates

How to run powershell commands on remote computers

By Isabella Wilson

SHORT DESCRIPTION

Explains how to use the “Run with PowerShell” feature to run a script from a file system drive.

LONG DESCRIPTION

Beginning in Windows PowerShell 3.0, you can use the “Run with PowerShell” feature to run scripts from File Explorer in Windows 8 and Windows Server 2012 and from Windows Explorer in earlier versions of Windows.

The “Run with PowerShell” feature is designed to run scripts that do not have required parameters and do not return output to the command prompt.

When you use the “Run with PowerShell” feature, the PowerShell console window appears only briefly, if at all. You cannot interact with it.

To use the “Run with PowerShell” feature:

In File Explorer (or Windows Explorer), right-click the script file name and then select “Run with PowerShell”.

The “Run with PowerShell” feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

It runs a command that has the following format:

“Run with PowerShell” sets the Bypass execution policy only for the session (the current instance of the PowerShell process) in which the script runs. This feature does not change the execution policy for the computer or the user.

The “Run with PowerShell” feature is affected only by the AllSigned execution policy. If the AllSigned execution policy is effective for the computer or the user, “Run with PowerShell” runs only signed scripts. “Run with PowerShell” is not affected by any other execution policy. For more information, see about_Execution_Policies.

Troubleshooting Note: Run with PowerShell command might prompt you to confirm the execution policy change.

How to run powershell commands on remote computers

PowerShell Remoting lets you run PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

PowerShell is locked-down by default, so you’ll have to enable PowerShell Remoting before using it. This setup process is a bit more complex if you’re using a workgroup instead of a domain—for example, on a home network—but we’ll walk you through it.

Enable PowerShell Remoting on the PC You Want to Access Remotely

Your first step is to enable PowerShell Remoting on the PC to which you want to make remote connections. On that PC, you’ll need to open PowerShell with administrative privileges.

In Windows 10, press Windows+X and then choose PowerShell (Admin) from the Power User menu.

How to run powershell commands on remote computers

In Windows 7 or 8, hit Start, and then type “powershell.” Right-click the result and choose “Run as administrator.”

How to run powershell commands on remote computers

In the PowerShell window, type the following cmdlet (PowerShell’s name for a command), and then hit Enter:

How to run powershell commands on remote computers

This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the cmdlet tells PowerShell to perform these actions without prompting you for each step.

If your PCs are part of a domain, that’s all the setup you have to do. You can skip on ahead to testing your connection. If your computers are part of a workgroup—which they probably are on a home or small business network—you have a bit more setup work to do.

Note: Your success in setting up remoting in a domain environment depends entirely on your network’s setup. Remoting might be disabled—or even enabled—automatically by group policy configured by an admin. You might also not have the permissions you need to run PowerShell as an administrator. As always, check with your admins before you try anything like this. They might have good reasons for not allowing the practice, or they might be willing to set it up for you.

Set Up Your Workgroup

If your computers aren’t on a domain, you need to perform a few more steps to get things set up. You should have already enabled Remoting on the PC to which you want to connect, as we described in the previous section.

Note: For PowerShell Remoting to work in a workgroup environment, you must configure your network as a private, not public, network. For more on the difference—and how to change to a private network if you already have a public network set up—check out our guide on private vs. public networks.

Next, you need to configure the TrustedHosts setting on both the PC to which you want to connect and the PC (or PCs) you want to connect from, so the computers will trust each other. You can do this in one of two ways.

If you’re on a home network where you want to go ahead and trust any PC to connect remotely, you can type the following cmdlet in PowerShell (again, you’ll need to run it as Administrator).

The asterisk is a wildcard symbol for all PCs. If instead you want to restrict computers that can connect, you can replace the asterisk with a comma-separated list of IP addresses or computer names for approved PCs.

After running that command, you’ll need to restart the WinRM service so your new settings take effect. Type the following cmdlet and then hit Enter:

How to run powershell commands on remote computers

And remember, you’ll need to run those two cmdlets on the PC to which you want to connect, as well as on any PCs you want to connect from.

Test the Connection

Now that you’ve got your PCs set up for PowerShell Remoting, it’s time to test the connection. On the PC you want to access the remote system from, type the following cmdlet into PowerShell (replacing “COMPUTER” with the name or IP address of the remote PC), and then hit Enter:

This simple command tests whether the WinRM service is running on the remote PC. If it completes successfully, you’ll see information about the remote computer’s WinRM service in the window—signifying that WinRM is enabled and your PC can communicate. If the command fails, you’ll see an error message instead.

How to run powershell commands on remote computers

Execute a Single Remote Command

To run a command on the remote system, use the Invoke-Command cmdlet using the following syntax:

“COMPUTER” represents the remote PC’s name or IP address. “COMMAND” is the command you want to run. “USERNAME” is the username you want to run the command as on the remote computer. You’ll be prompted to enter a password for the username.

Here’s an example. I want to view the contents of the C:\ directory on a remote computer with the IP address 10.0.0.22. I want to use the username “wjgle,” so I would use the following command:

How to run powershell commands on remote computers

Start a Remote Session

If you have several cmdlets you want to run on the remote PC, instead of repeatedly typing the Invoke-Command cmdlet and the remote IP address, you can start a remote session instead. Just type the following cmdlet and then hit Enter:

Again, replace “COMPUTER” with the name or IP address of the remote PC and replace “USER” with the name of the user account you want to invoke.

How to run powershell commands on remote computers

Your prompt changes to indicate the remote computer to which you’re connected, and you can execute any number of PowerShell cmdlets directly on the remote system.

How to run powershell commands on remote computers

Running commands locally on the remote computers is a common task many IT admins have to do every day. Here’s how you can do that in PowerShell.

It’s sometimes not enough to use DCOM, RPC, and other remote protocols to perform tasks on remote computers. To run commands on the actual remote computer requires a different approach. An admin must create a session of some kind and send commands across the network which are then consumed by the remote computer.

Since this is an article about PowerShell and not about telnet (you’re not using telnet, are you?), we’re going to stick with a technology called PowerShell Remoting. PowerShell Remoting is a feature of PowerShell that was introduced in Windows PowerShell v2. It allows users to open, manage and close sessions on remote computers and interact with them just as if they were sitting on the local console.

To establish a session called a PSSession with PowerShell requires that the remote computer has PowerShell Remoting enabled and listening on port 5985 (HTTP) or 5986 (HTTPS) with all of the usual firewall exceptions. Depending on how the user wants to establish the connection, some client configuration may be necessary as well. There are lots of different configurations a user may find themselves in, but for this article, we’re going to assume the local and remote computers are both in the same Active Directory domain. In this context, PowerShell Remoting will use Kerberos to authenticate, and we won’t need to pass alternate credentials to the commands.

Assuming that the remote computer has PowerShell Remoting enabled , let’s first try out running simple commands on a remote computer. As a test to ensure the code is running on the remote computer, I always like to use the hostname command which returns the name of the computer.

There are two ways we can run remote commands via PowerShell Remoting; we can issue commands interactively or non-interactively. Interactively means we’ll need to be at our computer to run the commands physically. Let’s start off by showing a non-interactive example.

To run remote commands non-interactively, we use the Invoke-Command command. This command has a ComputerName parameter that allows us to specify a computer to run on the command. We also have a ScriptBlock parameter where we’ll encapsulate the commands we intend to run on the remote computer. You can see below how this works in action. I’m running the hostname command locally on my computer and then running it remotely with Invoke-Command . You can see that it returns different results.

You’ve now run your first remote command! Anything can go into the ScriptBlock parameter. Below, I’m creating an if/then statement inside of the $scriptblock as a test to ensure the code is running remotely.

You may not have noticed but the Invoke-Command command created a lightweight session in the background to run that command and when the command was complete it tore it down. This is more obvious when you choose to run commands interactively using the Enter-PSSession command.

The Enter-PSSession command allows you to log into a remote session, run commands, get output and work just as if you were typing commands into the local console. To do this though, first, you need to establish a session. One way to do that is to use the ComputerName parameter. Notice below that I’ve entered a remote session and have a new PowerShell prompt that’s prepended by the computer name the session is running in.

At this point, I can run any commands I’d like, and they will be executed on the remote computer. We don’t need to use Invoke-Command to do this. We simply run the commands as they are. When we’re done, we have to close the session. To do that, we use the exit keyword which disconnects us from the session and closes it.

You can see below once we type exit and hit Enter, we’re immediately brought back to our local console.

PowerShell Remoting is a convenient way to execute commands on remote computers. As long as the remote computer is configured for PowerShell Remoting, which is a one-time setup, there is minimal, if any, setup needed on the client computer for this to work. Especially, if in an Active Directory environment, the process is seamless and a great way to prevent having to sneakernet all around the office just to run scripts and commands on all of your computers!

How to run powershell commands on remote computers

PowerShell Remoting lets you run PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

PowerShell is locked-down by default, so you’ll have to enable PowerShell Remoting before using it. This setup process is a bit more complex if you’re using a workgroup instead of a domain—for example, on a home network—but we’ll walk you through it.

Enable PowerShell Remoting on the PC You Want to Access Remotely

Your first step is to enable PowerShell Remoting on the PC to which you want to make remote connections. On that PC, you’ll need to open PowerShell with administrative privileges.

In Windows 10, press Windows+X and then choose PowerShell (Admin) from the Power User menu.

How to run powershell commands on remote computers

In Windows 7 or 8, hit Start, and then type “powershell.” Right-click the result and choose “Run as administrator.”

How to run powershell commands on remote computers

In the PowerShell window, type the following cmdlet (PowerShell’s name for a command), and then hit Enter:

How to run powershell commands on remote computers

This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the cmdlet tells PowerShell to perform these actions without prompting you for each step.

If your PCs are part of a domain, that’s all the setup you have to do. You can skip on ahead to testing your connection. If your computers are part of a workgroup—which they probably are on a home or small business network—you have a bit more setup work to do.

Note: Your success in setting up remoting in a domain environment depends entirely on your network’s setup. Remoting might be disabled—or even enabled—automatically by group policy configured by an admin. You might also not have the permissions you need to run PowerShell as an administrator. As always, check with your admins before you try anything like this. They might have good reasons for not allowing the practice, or they might be willing to set it up for you.

Set Up Your Workgroup

If your computers aren’t on a domain, you need to perform a few more steps to get things set up. You should have already enabled Remoting on the PC to which you want to connect, as we described in the previous section.

Note: For PowerShell Remoting to work in a workgroup environment, you must configure your network as a private, not public, network. For more on the difference—and how to change to a private network if you already have a public network set up—check out our guide on private vs. public networks.

Next, you need to configure the TrustedHosts setting on both the PC to which you want to connect and the PC (or PCs) you want to connect from, so the computers will trust each other. You can do this in one of two ways.

If you’re on a home network where you want to go ahead and trust any PC to connect remotely, you can type the following cmdlet in PowerShell (again, you’ll need to run it as Administrator).

The asterisk is a wildcard symbol for all PCs. If instead you want to restrict computers that can connect, you can replace the asterisk with a comma-separated list of IP addresses or computer names for approved PCs.

After running that command, you’ll need to restart the WinRM service so your new settings take effect. Type the following cmdlet and then hit Enter:

How to run powershell commands on remote computers

And remember, you’ll need to run those two cmdlets on the PC to which you want to connect, as well as on any PCs you want to connect from.

Test the Connection

Now that you’ve got your PCs set up for PowerShell Remoting, it’s time to test the connection. On the PC you want to access the remote system from, type the following cmdlet into PowerShell (replacing “COMPUTER” with the name or IP address of the remote PC), and then hit Enter:

This simple command tests whether the WinRM service is running on the remote PC. If it completes successfully, you’ll see information about the remote computer’s WinRM service in the window—signifying that WinRM is enabled and your PC can communicate. If the command fails, you’ll see an error message instead.

How to run powershell commands on remote computers

Execute a Single Remote Command

To run a command on the remote system, use the Invoke-Command cmdlet using the following syntax:

“COMPUTER” represents the remote PC’s name or IP address. “COMMAND” is the command you want to run. “USERNAME” is the username you want to run the command as on the remote computer. You’ll be prompted to enter a password for the username.

Here’s an example. I want to view the contents of the C:\ directory on a remote computer with the IP address 10.0.0.22. I want to use the username “wjgle,” so I would use the following command:

How to run powershell commands on remote computers

Start a Remote Session

If you have several cmdlets you want to run on the remote PC, instead of repeatedly typing the Invoke-Command cmdlet and the remote IP address, you can start a remote session instead. Just type the following cmdlet and then hit Enter:

Again, replace “COMPUTER” with the name or IP address of the remote PC and replace “USER” with the name of the user account you want to invoke.

How to run powershell commands on remote computers

Your prompt changes to indicate the remote computer to which you’re connected, and you can execute any number of PowerShell cmdlets directly on the remote system.

How to Stop Windows 10 Antivirus From Sending Files to Microsoft

What is a Simp?

How to Play or Convert a WebM File

How to Restore or Verify Default Services in Windows 7, 8, and 10

How to Save Your Favorites Places on Google Maps on Mobile and PC

Registry Editor Not Working, Opening, or Crashes

Windows Cannot Find C:\Windows\regedit.exe

Enable, Disable, Manage, Delete or Create a System Restore Point

Change Windows 10 Theme Without Activating Windows

How to Make Older Apps Compatible With Windows 10

Published by Timothy Tibbetts on 10/05/2020

Elevated Command Prompt or PowerShell, also known as ‘Run as Administrator,’ allows users to execute specific commands from DOS as Administrator in the Command Prompt or PowerShell. Here’s how.

TIP : Hard to fix Windows Problems? Repair/Restore Missing Windows OS Files Damaged by Malware with a few clicks

Microsoft is migrating everyone from the Command Prompt to Powershell. For the end-user, there’s no difference, but Windows 10 users should use PowerShell instead of the Command Prompt.

Because some commands require administrative privileges, there’s no harm in always running the Command Prompt or PowerShell as admin.

The easiest way to get to PowerShell is to press the Windows Key + X and click on Windows PowerShell (Admin).

Also, you can press the Windows key + S. This brings up the search and type in cmd for the command-prompt or Powershell. You should now see Command Prompt or Powershell as your choice. Click on Run as Administrator on the left or right-click and click Run as Administrator.

User Account Control (UAC) may pop up here. Click on yes.

That’s it. As you can see, the command prompt or Powershell now says Administrator: Command Prompt or Administrator: Windows PowerShell at the top.

Another neat shortcut you may like is the ability to switch back and forth from an elevated command prompt to normal by pressing Ctrl + Shift + Enter. Give it a try! This is a nice shortcut should you run the command prompt, then realize you needed to run it elevated or as administrator. Some people report this does not work, and we’re not sure why that is.

TIP : Hard to fix Windows Problems? Repair/Restore Missing Windows OS Files Damaged by Malware with a few clicks

To exit the Command Prompt, click on the X in the top right corner or type in exit.

How to run powershell commands on remote computers

How to run powershell commands on remote computers

  • OpenVPN IPv6 and IPv4 configuration – Mon, Mar 1 2021
  • 4sysops author and member competition 2020 – Fri, Jan 1 2021
  • Assign an IPv6 address to an EC2 instance (dual stack) – Tue, Dec 15 2020
  • Introducing PowerShell Remoting
  • Executing cmdlets with Invoke-Command
  • Testing if Remoting is enabled
  • Passing local variables to the remote session
  • Accessing methods in PowerShell Remoting

Introducing PowerShell Remoting ^

When it comes to managing remote computers with PowerShell, you have essentially three options. You can open an interactive session with the Enter-PSSession cmdlet (One-to-One Remoting). An alternative is the Invoke-Command cmdlet, which allows you to run remote commands on multiple computers (which is why it is called One-to-Many Remoting). The third option is to use one of those cmdlets that offer a ComputerName parameter. In most cases, PowerShell Remoting isn’t involved then. To list those cmdlets, you can use this command:

How to run powershell commands on remote computers

Cmdlets with the ComputerName parameter

In addition to the built-in cmdlets, quite a few PowerShell modules exist that support remote management. For instance, you can manage Active Directory from your workstation after installing the Active Directory module. Whenever one of these options exists, I would avoid enabling PowerShell Remoting on the remote machine.

Because PowerShell Remoting presents a security risk, it has to be enabled even within an Active Directory domain. You can also use Remoting on workgroup computers, but you will have to take care of a few extra configurations. If you don’t configure Remoting properly, you will run into errors such as WinRM cannot process the request or Access denied. In the latter case, you probably failed to provide admin credentials.

PowerShell Remoting depends on Windows Remote Management (WinRM), which is Microsoft’s implementation of the WS-Management (WS-Man) protocol. The protocol relies on HTTP or HTTPS and uses the TCP ports 5985 and 5986, respectively.

WS-Management encrypts all PowerShell communication even if you only work with HTTP. However, this kind of encryption is vulnerable to man-in-the middle attacks. I therefore recommended to use HTTPS instead of HTTP in insecure networks. I will say more about this topic in a follow-up post.

Executing cmdlets with Invoke-Command ^

Perhaps the most interesting form of PowerShell remote management is One-to-Many Remoting with the Invoke-Command cmdlet. This cmdlet allows you to execute PowerShell commands on multiple remote computers that contain cmdlets that don’t feature the -ComputerName parameter.

Two ways exist to connect to remote computers with Invoke-Command. You usually use the ‑ComputerName parameter to manage Windows machines. Alternatively, you can pass the ‑ConnectionUri parameter to manage backend applications, such as Exchange, or cloud services such as Azure.

In the remainder of this article, I will focus on managing remote Windows machines that are Active Directory domain members. The command below executes the Get-ChildItem cmdlet on the machine whose computer name is stored in the $RemoteComputer variable:

“Tell me and I forget, teach me and I may remember, involve me and I learn.” ― Benjamin Franklin.

How to Run PowerShell Commands on Remote Computers

PowerShell Remoting allows you to run individual PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

PowerShell is locked-down by default, so you’ll have to enable PowerShell Remoting before using it. This setup process is a bit more complex if you’re using a workgroup – for example, on a home network — instead of a domain.

Enabling PowerShell Remoting

On the computer you want to access remotely, open a PowerShell window as Administrator – right click the PowerShell shortcut and select Run as Administrator.

To enable PowerShell Remoting, run the following command (known as a cmdlet in PowerShell):

This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the command tells PowerShell to perform these actions without prompting you for each step.

Workgroup Setup

If your computers aren’t on a domain – say, if you’re doing this on a home network – you’ll need to perform a few more steps. First, run the Enable-PSRemoting -Force command on the computer you want to connect from, as well. (Remember to launch PowerShell as Administrator before running this command.)

On both computers, configure the TrustedHosts setting so the computers will trust each other. For example, if you’re doing this on a trusted home network, you can use this command to allow any computer to connect:

To restrict computers that can connect, you could also replace the * with a comma-separated list of IP addresses or computer names.

On both computers, restart the WinRM service so your new settings will take effect:

Testing the Connection

On the computer you want to access the remote system from, use the Test-WsMan cmdlet to test your configuration. This command tests whether the WinRM service is running on the remote computer – if it completes successfully, you’ll know that WinRM is enabled and the computers can communicate with each other. Use the following cmdlet, replacing COMPUTER with the name of your remote computer:

If the command completes successfully, you’ll see information about the remote computer’s WinRM service in the window. If the command fails, you’ll see an error message instead.

Executing a Remote Command

To run a command on the remote system, use the Invoke-Command cmdlet. The syntax of the command is as follows:

Invoke-Command -ComputerName COMPUTER -ScriptBlock -credential USERNAME

COMPUTER represents the computer’s name, COMMAND is the command you want to run, and USERNAME is the username you want to run the command as on the remote computer. You’ll be prompted to enter a password for the username.

For example, to view the contents of the C:\ directory on a remote computer named Monolith as the user Chris, we could use the following command:

Invoke-Command -ComputerName Monolith -ScriptBlock -credential chris

Starting a Remote Session

Use the Enter-PSSession cmdlet to start a remote PowerShell session, where you can run multiple commands, instead of running a single command:

Enter-PSSession -ComputerName COMPUTER -Credential USER

How to run powershell commands on remote computers

How to run powershell commands on remote computers

  • OpenVPN IPv6 and IPv4 configuration – Mon, Mar 1 2021
  • 4sysops author and member competition 2020 – Fri, Jan 1 2021
  • Assign an IPv6 address to an EC2 instance (dual stack) – Tue, Dec 15 2020
  • Introducing PowerShell Remoting
  • Executing cmdlets with Invoke-Command
  • Testing if Remoting is enabled
  • Passing local variables to the remote session
  • Accessing methods in PowerShell Remoting

Introducing PowerShell Remoting ^

When it comes to managing remote computers with PowerShell, you have essentially three options. You can open an interactive session with the Enter-PSSession cmdlet (One-to-One Remoting). An alternative is the Invoke-Command cmdlet, which allows you to run remote commands on multiple computers (which is why it is called One-to-Many Remoting). The third option is to use one of those cmdlets that offer a ComputerName parameter. In most cases, PowerShell Remoting isn’t involved then. To list those cmdlets, you can use this command:

How to run powershell commands on remote computers

Cmdlets with the ComputerName parameter

In addition to the built-in cmdlets, quite a few PowerShell modules exist that support remote management. For instance, you can manage Active Directory from your workstation after installing the Active Directory module. Whenever one of these options exists, I would avoid enabling PowerShell Remoting on the remote machine.

Because PowerShell Remoting presents a security risk, it has to be enabled even within an Active Directory domain. You can also use Remoting on workgroup computers, but you will have to take care of a few extra configurations. If you don’t configure Remoting properly, you will run into errors such as WinRM cannot process the request or Access denied. In the latter case, you probably failed to provide admin credentials.

PowerShell Remoting depends on Windows Remote Management (WinRM), which is Microsoft’s implementation of the WS-Management (WS-Man) protocol. The protocol relies on HTTP or HTTPS and uses the TCP ports 5985 and 5986, respectively.

WS-Management encrypts all PowerShell communication even if you only work with HTTP. However, this kind of encryption is vulnerable to man-in-the middle attacks. I therefore recommended to use HTTPS instead of HTTP in insecure networks. I will say more about this topic in a follow-up post.

Executing cmdlets with Invoke-Command ^

Perhaps the most interesting form of PowerShell remote management is One-to-Many Remoting with the Invoke-Command cmdlet. This cmdlet allows you to execute PowerShell commands on multiple remote computers that contain cmdlets that don’t feature the -ComputerName parameter.

Two ways exist to connect to remote computers with Invoke-Command. You usually use the ‑ComputerName parameter to manage Windows machines. Alternatively, you can pass the ‑ConnectionUri parameter to manage backend applications, such as Exchange, or cloud services such as Azure.

In the remainder of this article, I will focus on managing remote Windows machines that are Active Directory domain members. The command below executes the Get-ChildItem cmdlet on the machine whose computer name is stored in the $RemoteComputer variable:

Author: Peter Barnett Date: Mar 15, 2019

Fully functional for 50 endpoints, never expires. More details >

1. Modify a Script to Force Elevation

Add this snippet of code to the beginning of your PowerShell script, and a UAC prompt will appear, asking for administrative credentials or consent before any subsequent code is executed.

How to run powershell commands on remote computers

2. Run a Code from an Elevated Instance of the Windows PowerShell Integrated Scripting Environment (ISE)

Alternatively, you can run scripts directly from inside the Windows PowerShell ISE. To start the ISE with administrative privileges:

  • Switch to the Start menu in Windows,
  • Type powershell ise , and make sure that PowerShell ISE is selected in the search results. Press CTRL+SHIFT+ENTER to start the ISE with elevated privileges and enter administrative credentials or give sent if prompted.

How to run powershell commands on remote computers

In the PowerShell ISE window, select Open from the File menu to load your script.

How to run powershell commands on remote computers

  • Once the script is loaded into the ISE, press F5 to run the script.
  • The Windows PowerShell ISE is a useful environment for creating and editing your scripts. You have access to all the installed PowerShell modules and their related commands, plus troubleshooting tools.
  • 3. How to Use Windows PowerShell 2.0 Remoting

    PowerShell Remoting infrastructure is based on WinRM version 2.0, and therefore inherits all the advantages of this technology, such as encrypting the data transferred, and the ability to work on standard HTTP / HTTPS ports. But thanks to the rich capabilities of the language of Windows PowerShell, and its ability to work with objects, we get even greater opportunities.

    Before you take advantage of all these advantages, PowerShell Remoting needs to be activated on the manager and on managed computers. It’s easy to do this by running the cmdlet (Windows PowerShell command) Enable-PSRemoting . And if you add the -Force key, no confirmation will be requested. This cmdlet will call winrs quickconfig, if necessary, and create exceptions in Windows Firewall, so no further action is needed.

    After that, you can easily execute commands on other computers using the Invoke-Command cmdlet (or its alias icm)

    Invoke-Command -ComputerName Main -ScriptBlock c:\ipconfig.txt>

    Of course, the command can be placed in a variable in advance and for the -ComputerName parameter specify the names of not just one but several computers. The following sequence allows you to display the version of the Explorer.exe file from three computers at once.

    $Command = Invoke-Command -ComputerName Main, Server7, Replica -ScriptBlock $Command

    As you can see, you can send several commands in one block at once, place their execution results on several computers into a variable, and then process them at the workstation using the capabilities of Windows PowerShell to work with objects.

    However, the PowerShell Remoting features are just beginning here. Using the Enter-PSSession cmdlet, you can log in to an interactive Windows PowerShell session on a remote computer. You can exit this session by using the Exit-PSSession cmdlet or simply Exit.

    The New-PSSession cmdlet creates sessions on remote computers, pointers to which can be placed in a variable, and then passing it as an argument for Invoke-Command to execute commands on several computers at once in a persistent environment. You can see an example in the screenshot where I am running a sequence of commands on several computers at once from the list c: \ computers.txt.

    Consider using Action1 to run remote powershell script if:

    Action1 is a cloud-based platform for patch management, software deployment, remote desktop, software/hardware inventory, endpoint management and endpoint configuration reporting.

    Start your free trial or use free forever to manage up to 50 endpoints. More details >

    Relevant How To Articles and Action1 Features: