PowerTip: Use PowerShell to Run a Command on a Remote Server

Summary: Learn how to use Windows PowerShell to run a command on a remote server.

Hey, Scripting Guy! Question How can I run a command on a remote server by using Windows PowerShell Remoting?

Hey, Scripting Guy! Answer Use the Invoke-Command cmdlet, specify the computer name, and place the command in a script block:

Invoke-Command -ComputerName server1 -ScriptBlock {hostname}

Comments (5)

  1. i.biswajith says:

    WINRM should be enabled else it wont work.

  2. jrv says:


    Working with PowerSHell and remote processes is actually much easier than that:

    $process=Get-WmiObject -list Win32_Process -ComputerName MyPc -Credential domainuserid


    if($returnValue -eq 0){ $retunrValue.ProcessID

    With the CmdLet all of the heavy lifting is done for you even out of a domain or across domains and forests.

  3. winrm must be enabled in remote server is it right biswajith?

  4. scooter says:

    This is great if you work in a perfect world where every machine is running WinRM aand is on one domain.  Unfortunately I don't work in such an environment and have to resort to creating a remote process.  Something like this:

         $con_ops = new-object management.connectionoptions

         $con_ops.Username = $local:textusr

         $con_ops.SecurePassword = (ConvertTo-SecureString -AsPlainText -String $local:textpw -Force )

         $scope = new-object management.managementscope "\$remote_hostrootcimv2",$con_ops


         $management_path = new-object management.managementpath "win32_process"

         $object_get_options = new-object management.objectgetoptions

         $remote_process = new-object management.managementclass      $scope,$management_path,$object_get_options

         $installPID = ($remote_process.Create($install_file)).ProcessID

  5. Anik says:

    How can we run "set share =:" command on a remote system?

Skip to main content