How To Run Commands in PowerShell Remote Computers? Best Example

PowerShell Remote is a powerful feature that enables administrators to manage multiple machines from a central location, providing a seamless and efficient approach to system administration. Whether you need to manage servers, workstations, or network devices, PowerShell Remote allows you to execute commands, manage configurations, and troubleshoot issues remotely. 

How To Run Commands in PowerShell Remote Computers

In this comprehensive guide, we will explore the ins and outs of PowerShell Remote, covering its benefits, setup, and usage to empower you with the knowledge to streamline your administrative tasks.

Windows PowerShell Remote Over WinRM Enabled

You may have selected the option to enable PowerShell Remote when installing PowerShell 7. To be safe, however, use the instructions below to activate remote PowerShell.

  • Log on to the distant Windows 10 computer.
  • Launch PowerShell in administrative mode.
  • To activate remote PowerShell, run the command below.
Enable-PSRemoting

Enabling Remote PowerShell on Linux via SSH

Unlike WinRM, which is exclusive to Windows, you can enable PowerShell Remote on Linux using SSH. If you have already installed PowerShell, follow the steps below:

  • Log in to your remote Linux system and open a terminal session.
  • Open the SSH server configuration file in a text editor of your choice, such as Gedit, Sublime, or Nano.
sudo nano /etc/ssh/sshd_config
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo
  • Close the text editor to save the changes.
  • Finally, restart the SSH server to apply the changes:
sudo systemctl restart sshd
  • Now, remote PowerShell access to your Linux system via SSH is enabled and ready to use.

On the Local Machine, Add Remote PowerShell Trusted Hosts

Your PowerShell Remote computer is now prepared to accept commands. However, if the remote PowerShell hosts are not included in the list of Trusted Hosts, your management machine might reject the connection. You must add the remote hosts to your Trusted Hosts in order to prevent potential remote access problems.

  • Open PowerShell on your management PC as an administrator.
  • Run the winrm command below. With this command, you can add the Linux (192.168.8.xxx) and Windows (192.168.8.xxx) machines you’re using remotely to your computer’s trusted hosts list.
    • winrm set winrm/config/client ‘@{TrustedHosts=” 192.168.8.171,192.168.8.xxx”}’
winrm set winrm/config/client '@{TrustedHosts

Command Execution on Remote Computers

You will run commands on the PowerShell Remote system in this part after everything is in place.

  • On your management PC, start a fresh PowerShell session.
  • Use the Invoke-Command command listed below to list the first five services as user ma on the remote system. 
  • The ScriptBlock switch’s brackets contain the command to execute, as seen below.
    • Invoke-Command
    • -ComputerName 192.168.8.xxx
    • -Credential ‘ma’
    • -ScriptBlock { Get-Process | Select-Object -First 5 }
  • Use the command below to carry out the same action on a remote Linux machine instead. Take note of the substitution of -UserName for -ComputerName and -HostName for -ComputerName. 
  • By using these options, the Invoke-Command cmdlet can be informed that the target is an SSH host.
    • Invoke-Command
    • -HostName 192.168.8.xxx
    • -UserName ‘test’
    • -ScriptBlock { Get-Process | Select-Object -First 5 }

Script Execution on Remote Computers

Using the Invoke-Command cmdlet, you can easily execute a PowerShell script file on a PowerShell Remote machine. However, you’ll specify the -FilePath option followed by the script’s local path rather than the -ScriptBlock parameter.

  • On your local computer, create a PowerShell script file called nametime.ps1.
  • Add the following code to the script after opening it in your favorite script editor. After that, save the file.
  • # nametime.ps
    • Hostname
    • Get-Date
  • Use the -FilePath option to run the Invoke-command cmdlet. The script file should be indicated by the -FilePath.
  • # Run the remote PowerShell script via WinRM
    • Invoke-Command
    • -ComputerName 192.168.8.xxx
    • -Credential ‘ma’
    • -FilePath .\nametime.ps1
  • # Use SSH to launch the script on the remote PowerShell computer.
    • Invoke-Command
    • -HostName 192.168.8.xxx
    • -UserName ‘test’
    • -FilePath .\nametime.ps1

Interacting with a Remote Computer

You learned how to execute certain commands on a PowerShell Remote machine. You will learn how to launch an interactive session in this section so that you can issue various powershell commands to the remote machine as if it were local.

  • Start a remote session as user ma on the computer located at 192.168.8.xxx by running the Enter-PSSession command.
Enter-PSSession -ComputerName 192.168.8.xxx -Credential ma
  • A new prompt will appear, as displayed below. The remote PowerShell host is identified by its IP address, which is enclosed in square brackets.
  • The dir command can be used to list the files in the active working directory. Within a session dir, you can run as many PowerShell commands as you’d like.
Dir
  • Your screen should display the directory’s contents, as shown in the screenshot below.
  • Once you’ve completed managing the remote system, run the Exit-PSSession cmdlet listed below to end the current session.
Exit-PSSession

Remote PowerShell Sessions: Cutting and Reconnecting

As an alternative to interactive sessions or one-off commands, it is possible to preserve a session permanently. You can transition between sessions without losing context if you store a session.

  • Use the New-PSSession cmdlet to establish a session as user ma (-Credential) on the remote computer at 192.168.8.xxx. As seen, save the session in the $var variable.
$var = New-PSSession -ComputerName 192.168.8.xxx -Credential ma
  • To inspect the contents of the variable $var, use the following command.
$var
  • A session object with a RemoteMachine ComputerType and State set to Opened should be visible.
  • To access the session that is saved in $var, issue the Enter-PSSession command.
Enter-PSSession $var
  • If everything is in order, the prompt will switch to a remote prompt and stop producing output.
  • Make a variable called $rem1 in the session. Use any suitable variable name and identifier string. This variable will be used later to check if you are linked to the same session.
$rem1 = "same session"
Enter-PSSession $var
$rem1
  • Before ending the PowerShell Remote session, you should see the same value that you previously set, indicating that you have successfully reconnected to the same session.
  • To end the session once more, issue the Exit-PSSession command as shown below.
Exit-PSSession
  • To display the hostname of the remote computer in the same remote session, execute Invoke-Command with the session switch set to $var. 
  • This approach is a substitute for manually joining, detaching, and reconnecting to a single session in order to execute instructions within the same session.
ScriptBlock hostname> -Session $var -Invoke-Command
Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.