How to Run Commands on A Remote Computer Using Psexec And Powershell?

16 minutes read

Using PsExec and PowerShell to run commands on a remote computer involves a series of steps that leverage the capabilities of each tool to execute tasks remotely. PsExec is a command-line utility that allows you to execute processes on remote systems. It is part of the Sysinternals suite by Microsoft. To use PsExec, you generally need administrative access to the remote system. First, you download PsExec from the Sysinternals website and extract it. To run a command on a remote computer using PsExec, you open a command prompt on your local machine and use a syntax like psexec \\remote_computer_name -u username -p password command_to_run, replacing remote_computer_name, username, password, and command_to_run with the appropriate values. Ensure that the remote computer is reachable over the network and that file and printer sharing are enabled. PsExec uses ports associated with file and printer sharing for its functionality.


PowerShell offers native remoting capabilities with more flexibility and security than PsExec. To run commands on a remote computer using PowerShell, you enable PowerShell remoting on the target machine, which can be done by running Enable-PSRemoting -Force in a PowerShell session with administrative privileges. This command configures the necessary settings and firewall rules to allow remote connections. On your local machine, to execute a command remotely, you use Invoke-Command -ComputerName remote_computer_name -Credential (Get-Credential) -ScriptBlock { command_to_run }, replacing remote_computer_name and command_to_run with the appropriate details. You'll be prompted to enter a username and password with sufficient privileges on the remote computer. The -Credential parameter is used to specify the user account that has administrative privileges on the remote system.


Since PsExec and PowerShell involve different mechanisms and security considerations, choose the one that best suits your needs based on your network environment and security requirements. Ensure that you comply with your organization's policies when executing remote commands.

Best Powershell Books to Read in February 2025

1
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 5 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

2
PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

Rating is 4.9 out of 5

PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

3
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 4.8 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

4
Learn PowerShell Scripting in a Month of Lunches

Rating is 4.7 out of 5

Learn PowerShell Scripting in a Month of Lunches

5
Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

Rating is 4.6 out of 5

Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

6
Windows PowerShell in Action

Rating is 4.5 out of 5

Windows PowerShell in Action

7
Windows PowerShell Step by Step

Rating is 4.4 out of 5

Windows PowerShell Step by Step

8
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.3 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


What are the limitations of PsExec when compared to other remote execution tools?

PsExec is a popular command-line tool from Microsoft's Sysinternals suite, used for executing processes on remote systems. While it has many advantages, it also comes with several limitations compared to other remote execution tools:

  1. Limited Platform Support: PsExec is primarily designed for Windows environments. It doesn't natively support non-Windows platforms like Linux or macOS, which means it's not suitable for heterogeneous environments.
  2. No Built-in Encryption: PsExec does not encrypt the communication between the client and the remote system. This can be a security concern, especially if used over untrusted networks. Other tools may offer encrypted communication to enhance security.
  3. Dependency on SMB: PsExec relies on Server Message Block (SMB) protocol for connectivity. If SMB is disabled or blocked by a firewall in your environment, PsExec will not function.
  4. Basic Authentication: It uses Windows authentication mechanisms, which might not be as flexible as the more advanced authentication and authorization options provided by other tools that support different credential stores or multifactor authentication.
  5. Limited to Command Line: PsExec is primarily a command-line tool with no graphical user interface (GUI). This can be a limitation for users who prefer or require a GUI for managing remote sessions.
  6. No Native Scripting or Automation Support: While you can use PsExec in scripts, it doesn't have built-in scripting language support or automation features out of the box. Other tools, like Ansible or PowerShell Remoting, offer more robust scripting capabilities.
  7. Limited Process Management: PsExec can start and manage processes remotely, but it doesn't have the advanced process management capabilities of some other tools, which might allow for scheduling, pausing, or conditional execution.
  8. Complex Error Handling: Error messages from PsExec can be vague, making troubleshooting more challenging. Other tools might provide more detailed logging and error reporting.
  9. No Session Persistence: PsExec does not maintain session state across connections, which can be a limitation if you need persistent sessions for ongoing management tasks.
  10. Scalability: While effective for small-scale or occasional remote commands, PsExec lacks the scalability features of dedicated configuration management tools that can handle operations across thousands of systems efficiently.


By understanding these limitations, users can better decide when PsExec is suitable for their needs or when another remote execution tool might be more appropriate.


How to securely run PowerShell scripts on remote computers?

Running PowerShell scripts on remote computers securely involves several best practices and configurations to ensure both security and proper execution. Below are some steps and considerations to help you achieve this:

1. Use PowerShell Remoting

  • Enable Remoting: Ensure that PowerShell Remoting is enabled on both the local and remote machines using the Enable-PSRemoting cmdlet.
  • Use SSH for Remoting: If you're on PowerShell Core (v6+), consider using SSH for remoting instead of WinRM to enhance security.

2. Authentication and Authorization

  • Use Secure Authentication: Employ Kerberos or certificate-based authentication for securing the connection. Avoid using Basic authentication over HTTP.
  • Non-Admin Access: Use constrained endpoints to limit the commands and access available to the script. Create a special remote session configuration file that restricts users' access to specific cmdlets or functions.
  • JEA (Just Enough Administration): Implement JEA to limit administrative privileges by providing only the necessary capabilities to specific users.

3. Secure the Connection

  • SSL/TLS: Configure WinRM to use HTTPS instead of HTTP. This requires configuring certificates on the remote computers.
  • Firewall Rules: Ensure that your firewall allows traffic only from specific IP addresses if applicable.

4. Script Security

  • Code Signing: Sign your PowerShell scripts with a trusted certificate to verify their integrity and authenticity.
  • Execution Policy: Set an appropriate execution policy. Although it's not a security boundary, setting it to AllSigned or RemoteSigned helps in mitigating risks from running untrusted scripts.

5. Least Privilege Principle

  • Run scripts with the least privileged account possible. Avoid using accounts with unnecessary administrative privileges.

6. Logging and Monitoring

  • Enable Logging: Enable PowerShell logging through Group Policy to keep track of any script executions and remoting sessions.
  • Monitor Activity: Regularly review logs to detect any unauthorized or suspicious activities.

7. Keep Systems Updated

Regularly update both the PowerShell version and the operating systems on remote machines to mitigate vulnerabilities.

8. Use Secure Code Practices

  • Avoid storing sensitive information in scripts. Use the SecureString class or a vault.
  • Use parameter validation to ensure inputs are sanitized and within expected ranges.


By following these practices, you can enhance the security of running PowerShell scripts on remote computers and protect your systems from potential threats.


How to execute PowerShell scripts with administrative privileges remotely?

Executing PowerShell scripts with administrative privileges remotely can be accomplished in a few different ways, depending on your environment and specific requirements. Here are some common methods:

  1. PowerShell Remoting (WinRM): PowerShell Remoting allows you to run scripts or commands on remote machines. Ensure that PowerShell Remoting is enabled on the target machine using the command Enable-PSRemoting -Force. To execute a script with administrative privileges, you might need to use a tool like Invoke-Command with the -Credential parameter to specify an account with the necessary permissions: $credential = Get-Credential Invoke-Command -ComputerName -ScriptBlock { & "C:\Path\To\Script.ps1" } -Credential $credential
  2. PSExec from Sysinternals: You can use PSExec, a tool from Sysinternals, to run programs on remote systems. You need to specify administrative credentials, and it allows you to run PowerShell scripts: psexec \\ -u -p powershell -ExecutionPolicy Bypass -File "C:\Path\To\Script.ps1" Note: Be cautious with passing passwords in the command. Consider using more secure methods to handle credentials.
  3. Task Scheduler: You can remotely create and trigger a scheduled task on the target machine to execute a PowerShell script with the highest privileges. Use scripts or Group Policy to deploy and run the task.
  4. WinRM HTTPS for Secure Connections: For secure remote execution, configure WinRM over HTTPS to encrypt data being sent. You'll need to configure HTTPS listeners and manage certificates, which adds security, especially across different networks.
  5. Run with a Service Account: Configure a service account with administrative privileges on the target machines. Use this account for remote administration tasks, including executing PowerShell scripts. Ensure the account is used safely and with access controls.
  6. Deployment Tools: Consider using enterprise-grade tools such as Microsoft System Center Configuration Manager (SCCM) or Ansible for large-scale script deployment and execution with administrative privileges across many machines.


Important Security Considerations:

  • Always ensure that your administrative scripts and tools are encrypted and that credentials are handled securely.
  • Limit administrative access to only necessary users and use the principle of least privilege.
  • Use logging and monitoring to track remote script executions for audit and troubleshooting purposes.


By leveraging these methods, you can effectively manage and execute PowerShell scripts across remote systems securely and efficiently.


What is the difference between PsExec and WinRM?

PsExec and WinRM (Windows Remote Management) are both tools used for remote administration of Windows systems, but they operate differently and have their own use cases and features. Here’s a comparison of the two:

PsExec

  1. Type: PsExec is a command-line tool that is part of the Sysinternals Suite, developed by Microsoft.
  2. Functionality: It allows you to execute processes on remote systems and redirect the console output back to the local system, effectively allowing for remote command execution.
  3. Protocol: PsExec uses SMB (Server Message Block) for communication.
  4. Ease of Use: It is relatively simple to use for basic tasks and doesn’t require much initial setup on the remote system.
  5. Security: It requires administrative privileges on the remote system and typically passes credentials in clear text over the network unless additional steps are taken to secure it. Not inherently encrypted.
  6. Dependencies: It doesn’t require additional services to be configured on the remote machine, other than allowing for administrative shares.
  7. Usability: Often used for simpler or legacy setups, quick tasks, and where environments are already configured with necessary permissions.
  8. Limitations: Lacks some advanced features for scripting and managing outputs compared to other remote management solutions like PowerShell Remoting.

WinRM

  1. Type: WinRM is a feature of Windows operating systems that allows for remote management operations via the WS-Management protocol.
  2. Functionality: It enables the execution of commands, scripts, and Shell sessions on remote machines. It is a more comprehensive tool for managing Windows systems remotely.
  3. Protocol: Uses HTTP/HTTPS for communication, which can be more secure than SMB if properly configured with encryption.
  4. Ease of Use: May require more initial setup and configuration compared to PsExec. WinRM may need to be enabled explicitly, and proper permissions must be configured.
  5. Security: Supports encrypted communications over HTTPS and Kerberos or NTLM for authentication, offering more secure interactions.
  6. Dependencies: Requires the WinRM service to be enabled and configured on the remote machine, as well as potentially needing firewall ports to be open (default is port 5985 for HTTP and 5986 for HTTPS).
  7. Usability: More suitable for automation, scripts, and environments where PowerShell Remoting is utilized due to its robust handling of outputs and integration with PowerShell.
  8. Advanced Features: Offers more advanced features for automation, such as being able to pass complex inputs, handle output robustly, integrate with PowerShell Remoting, and manage many hosts at once.


In summary, PsExec is often favored for its simplicity and minimal setup requirements for one-off tasks, while WinRM is a more powerful and secure option suitable for enterprise environments and extensive automation tasks.


What is PsExec and how does it work?

PsExec is a lightweight telnet-replacement utility that allows users to execute processes on remote systems. It is part of the Sysinternals Suite, which was created by Mark Russinovich and is now owned by Microsoft. PsExec is frequently used by system administrators and IT professionals to remotely manage and troubleshoot systems without needing to physically access them.

How PsExec Works:

  1. Deployment: PsExec can be run from the command line on a local system. You specify the remote system's name or IP address and the command you want to execute on that system.
  2. Authentication and Connectivity: For PsExec to connect to a remote system, it typically requires administrative privileges on that system. You can specify a username and password or use the credentials of the current logged-in user.
  3. Communication: PsExec uses the Server Message Block (SMB) protocol to connect to the remote system and execute commands. Since SMB is often blocked by firewalls, network policies might need adjustment to allow its operation.
  4. Temporary Service: Upon execution, PsExec creates a temporary service on the remote machine to run the specified command. Once the command has finished executing, PsExec removes this temporary service.
  5. Output Redirection: The standard output and standard error streams from the command executed remotely can be redirected back to the local system, allowing the user to view the results.
  6. Error Handling: PsExec returns error codes based on the execution status of the remote command, allowing scripts and applications to react programmatically to different outcomes.

Use Cases:

  • Remote Administration: PsExec is used for tasks like installing software, updating patches, or managing configurations on remote Windows systems.
  • Scripting Automation: It can be included in scripts to automate maintenance tasks across multiple systems.
  • Troubleshooting: It allows execution of diagnostic or recovery commands without the need to establish a full remote desktop session.

Security Considerations:

  • PsExec requires administrative rights, so improper use or malicious exploitation can lead to security risks.
  • Ensuring that secure and encrypted methods are used for credential transmission (e.g., using Kerberos instead of NTLM) is crucial to prevent unauthorized access.
  • It is often flagged by security tools as a potential concern for misuse scenarios, such as lateral movement in cyber attacks.


Overall, PsExec is a powerful tool for IT professionals, though its usage should be carefully managed to mitigate potential security risks.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To pull changes from a remote repository in Git, you can follow these steps:First, ensure you are in the local repository where you want to pull the changes.Use the command git remote -v to check if the remote repository is already added. This will show the li...
Once connected to a device via PowerShell, you can send CLI commands by using the "Invoke-Command" cmdlet. This cmdlet allows you to run commands on a remote machine, making it easy to send CLI commands once a connection is established. Simply specify ...
To switch to a new remote repository in Git, you first need to remove the existing remote repository using the command: git remote remove originThen, you can add a new remote repository using the command: git remote add origin Finally, you can push your local ...
To push changes to a remote repository in Git, follow these steps:First, make sure you have committed your changes locally using git commit. This creates a snapshot of the changes you want to push.Ensure you have added a remote repository using git remote add ...
To remove a remote URL from a GitHub repository, you can use the git remote rm command in your terminal. Simply navigate to the directory of your local repository and run the following command:git remote rm <remote_name>Replace <remote_name> with t...
To enable remote access on a Windows Mini PC, you can use the built-in Remote Desktop feature. First, you need to make sure that both the host Mini PC and the remote computer are connected to the same network. Then, go to the Control Panel on the Mini PC and n...