Wednesday 9 September 2015

How to connect Powershell remotely and execute command

The thing is it's very simple to connect powershell to remote computer if the machine is available with your network. Since we all know that Powershell is a powerful tool it allows you to execute commands and scripts remotely. Here the things which explains about the remote connectivity.

First you need to enable few things in your remote machine which you want to connect it .

It's always better to launch powershell to launch with Administrator privilege to make configurations.




Execute the Command,   Enable-PSRemoting -Force which makes the remote machine to connect for remote powershell. Here -Force is the property of this command which avoids prompting fro remote connection every time.



Next step is to add which are all the machines you want to access powershell with the remote machine . This option is simple that we can add limited machines or may can have all the machines within the network .

For this execute the command , Set-Item wsman:\localhost\client\trustedhosts * . IF you ant to restrict the machines then you can give the machine IP's or Host name as a comma separated.


Once dine with these things you have to restart the WinRM service. WinRM servce is noting but windows Remote management Service which is required for all remoting activities. In order to restart the service execute the command , Restart-service WinRM -Force


OK What is the next step . Say it's Done. How shall I check whether the configuration is working fine or not . For that there is a command to check whether the remote connection to the specific system is fine. Get to your machine once done with the remote machine.

Execute the command Test-WSMan Host/IPAddress . If is configuration is fine then it will show information about the schema and version or in other case if it fails to connect then it will show some error message with red.




These are all common things for remote powershell connectivity but few more things are there which will make your head to rotate. That is what it may starts with port problem.Because we will always connect remotely using port that may not visible usually.

 But if it is configured with different port or not the default port then there will be a problem . So we are not done with the configuration. For that what we need to do is , we hve to get the port which is listening over the remote machine . To gt the listening port execute the following command in remote machine , winrm e winrm/config/listener


Now you got the port, Test it using the command Test-WSMan Host/IPAddress -Port <Port-Listening st Remote Machine>

Note: By default the WinRM listening port will be 5895 for http

If you done fine with all the above configuration then there will be no failing case at all.

Simple command to get list of process running on remote machine:

Testing is fine.Connectivity is fine. Then , we can start to do things we need from remote machine. For that execute the commands.

To login wit the remote machine you should have the credentials . In powershell the beauty is we can save the Credential within a single variable.

$Credential = Get-Credential




the above command will prompt for user name and password . Give the exact user name with domain name with slash before use name if it is there . Click OK to save it. Now the $Credential variable have the credential information. Then proceed for the command which will give you the data of remote machine.

Invoke-Command -Computer COMPUTERNAME -Port <Listening Port> -Credential $Credential -ScriptBlock{Get-Process} 

This will list processes running in the emote machine.What is the command given above is exactly doing. Lest's make you clear with that. In that command Invoke-Command used to invoke remote command or script. The man thing is the -ScriptBlock and the content which we are giving within the curly braces. We can also give the exact  script path within the curly braces if you want to execute a script rather than running as a command in the remote powershell.


No comments:

Post a Comment