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

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

  • Comments 5
  • Likes

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}

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • WINRM should be enabled else it wont work.

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

  • 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_host\root\cimv2",$con_ops

         $scope.Connect()

         $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

  • @scooter.

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

    $process=Get-WmiObject -list Win32_Process -ComputerName MyPc -Credential domain\userid

    $retunValue=$process.Create($install_file)

    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.

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