{"id":16491,"date":"2010-11-16T00:01:00","date_gmt":"2010-11-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/11\/16\/enable-powershell-remoting-to-enable-running-commands\/"},"modified":"2010-11-16T00:01:00","modified_gmt":"2010-11-16T00:01:00","slug":"enable-powershell-remoting-to-enable-running-commands","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/enable-powershell-remoting-to-enable-running-commands\/","title":{"rendered":"Enable PowerShell Remoting to Enable Running Commands"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><b>Summary:<\/b> Learn how to enable Windows PowerShell remoting to allow execution of commands on remote computers.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/span>Hey, Scripting Guy! I need to be able to run commands on remote computers. I have <a href=\"http:\/\/support.microsoft.com\/kb\/968929\">Windows PowerShell 2.0<\/a>, is there something in that I can use?<\/p>\n<p>&#8212; ML<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/span>Hello ML, Microsoft Scripting Guy Ed Wilson here. I got back late last night from <a href=\"http:\/\/en.wikipedia.org\/wiki\/Columbia,_South_Carolina\">Columbia, South Carolina<\/a> because I was speaking to the SQL Users group down there. It is a nice drive at night, and generally not too much traffic. The <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife<\/a> went with me, and to be honest I believe they were more excited about talking to her than to me but that is fine, I like to talk to her as well. The group asked lots of questions, and I only made it to my second slide in my Microsoft PowerPoint deck, but hey I only carry that deck around with me so I have something to turn in for conference abstracts. I rarely follow it. For me, one of the great things about Windows PowerShell is that I can solve a problem with a few lines of code, and to show SQL DBA&#8217;s (Database Administrators) that I can do that in an interactive manner, without having to run a script is a powerful message. Therefore, most of my presentation is impromptu, and it has a tendency to morph in the direction of the audience&#8217;s interest (Note: I do not advocate this as a recommended presentation approach. I am merely relating how I approach informal user group meetings). The meeting room filled. There was standing room only. I had met several of the attendees at <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/05\/use-powershell-to-graph-the-reliability-of-windows-2008-r2.aspx\">SQL Saturday<\/a> and therefore it was great to renew acquaintances. This morning I actually had some questions that continued last night&#8217;s discussion. It really is great to get out of the house, and to talk with people who are learning Windows PowerShell.<\/p>\n<p>ML, to be able to use Windows PowerShell remoting, you have to first enable it. This is because Windows PowerShell ships in a locked down configuration. You cannot run scripts, and you cannot use remoting. <\/p>\n<p>The easiest way to enable Windows PowerShell remoting is to use the <b>Enable-PSRemoting<\/b> cmdlet. To do this, you have to launch Windows PowerShell with Admin rights. If you do not have Admin rights when you run Windows PowerShell, the following error occurs.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; Enable-PSRemoting -Force<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Enable-PSRemoting : Access is denied. You have to run this cmdlet from an elevated p<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">rocess.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">At line:1 char:18<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">+ Enable-PSRemoting &lt;&lt;&lt;&lt;&nbsp; -Force<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [Enable-PSRemoting], InvalidOperati<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp; onException<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp; .Commands.EnablePSRemotingCommand<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>To make sure Windows PowerShell is running with Admin rights, right-click the <b>Windows PowerShell icon<\/b>, and select <b>Run as Administrator<\/b>, seen in the following figure, if you are running <a href=\"http:\/\/www.microsoft.com\/windows\/windows-7\/default.aspx\">Windows 7<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5672.HSG-11-16-10-01.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>On <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=6ccb7e0d-8f1d-4b97-a397-47bcc8ba3806\">Windows XP<\/a>, you can use the <b>Run as&#8230;<\/b> feature that you can access by holding down the <b>Shift Key<\/b> while right-clicking on the <b>Windows PowerShell icon<\/b>. The <b>Run As&#8230;<\/b> menu is seen in following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.HSG-11-16-10-02.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>After you have Windows PowerShell running with Admin rights, use the <b>Enable-PSRemoting<\/b> Windows PowerShell cmdlet to automatically configure WinRM, the firewall, and the WinRM service to enable Windows PowerShell remoting to work. If you want to be prompted before each change, do not use any switches when you run the Windows PowerShell cmdlet. If you do not want to be prompted <a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Force_(Star_Wars)\"><i>use the force<\/i><\/a> parameter as seen here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Enable-PsRemoting -Force<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>When you run the cmdlet without the switch, you are prompted several times during the configuration of Windows PowerShell (an activity that usually takes less than a minute to complete). This is seen in the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2816.HSG-11-16-10-03.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>After the configuration is complete, I like to perform a quick test to make sure that everything has worked correctly. First, I use <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ping\">Ping<\/a> to make sure that my computer can resolve the remote host, and then I use the <b>invoke-command<\/b> Windows PowerShell cmdlet. When you use the <b>invoke-command<\/b> Windows PowerShell cmdlet, I have to give it the name of the remote computer, and the command to execute. The command to execute is put in the <i>scriptblock<\/i> parameter. The syntax of this command is seen here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Invoke-Command -computername [COMPUTER} -ScriptBlock { COMMAND }<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The <i>scriptblock<\/i> parameter accepts your code to run, and is enclosed inside a pair of braces. As seen here, I first ping the remote computer, and then execute the <b>hostname<\/b> command on the remote computer. This guarantees that I am working with the computer that I think I am working upon. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; ping teresa-kitchen<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Pinging teresa-kitchen.NWTraders.Com [192.168.1.89] with 32 bytes of data:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Reply from 192.168.1.89: bytes=32 time&lt;1ms TTL=128<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Reply from 192.168.1.89: bytes=32 time&lt;1ms TTL=128<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Reply from 192.168.1.89: bytes=32 time&lt;1ms TTL=128<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Reply from 192.168.1.89: bytes=32 time&lt;1ms TTL=128<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Ping statistics for 192.168.1.89:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Approximate round trip times in milli-seconds:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;&nbsp;&nbsp; Minimum = 0ms, Maximum = 0ms, Average = 0ms<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; Invoke-Command -ComputerName teresa-kitchen -ScriptBlock { hostname }<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">teresa-kitchen<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">If I have to specify alternative credentials for the remote command, I use the <i>credential<\/i> parameter as seen here.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; Invoke-Command -ComputerName teresa-kitchen -ScriptBlock { hostname } -Creden<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">tial nwtraders\\administrator<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">teresa-kitchen<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>When the command runs, the <b>Windows PowerShell Credential Request<\/b> dialog box seen in the following figure appears.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0871.HSG-11-16-10-04.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In reality, I only have to type my alternative credentials once because I can store them in a variable. In this example, I use the <b>Get-Credential<\/b> Windows PowerShell cmdlet to retrieve my alternative credentials. I store the credential object in the <b>$credential<\/b> variable. I then use those credentials to run commands on two different computers. This is seen here. The following commands are single line commands. I used line continuation to break the command onto two lines due to display limitations on the blog. If you are using a similar command, you will not have to use the&nbsp;backtick &nbsp;(`) character for line continuation.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; $credential = Get-Credential -Credential nwtraders\\administrator<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; Invoke-Command -ComputerName teresa-kitchen -ScriptBlock { hostname } `<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">-Credential $credential<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">teresa-kitchen<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt; Invoke-Command -ComputerName hyperv -ScriptBlock { hostname } `<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">-Credential $credential<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">HyperV<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">PS C:\\&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>If you are working in a networked setting and you want to enable Windows PowerShell remoting on all computers in a forest, domain, or organizational unit, you can use <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/group+policy\/\">Group Policy<\/a> to make the configuration changes. Unfortunately, there is no <b>Enable-PSRemoting<\/b> Group Policy object. The WinRM service is configurable through Group Policy and is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa384372(VS.85).aspx\">well documented on MSDN<\/a>. The Group Policy settings are seen in the following figure, Windows PowerShell remoting relies on more than just WinRM. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6507.HSG-11-16-10-05.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>One way to get the advantage of Group Policy and the advantage of using the <b>Enable-PSRemoting<\/b> cmdlet is to use Group Policy to specify a startup script. This is seen in the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8130.HSG-11-16-10-06.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The script is a single line, saved in a .ps1 file. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Enable-PSRemoting -Force<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>ML, that is all there is to using configuring and to using Windows PowerShell Remoting. Remoting week will continue tomorrow when I will talk about how to establish remote sessions. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to enable Windows PowerShell remoting to allow execution of commands on remote computers. &nbsp; Hey, Scripting Guy! I need to be able to run commands on remote computers. I have Windows PowerShell 2.0, is there something in that I can use? &#8212; ML &nbsp; Hello ML, Microsoft Scripting Guy Ed Wilson [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,57,3,4,45],"class_list":["post-16491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-remoting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to enable Windows PowerShell remoting to allow execution of commands on remote computers. &nbsp; Hey, Scripting Guy! I need to be able to run commands on remote computers. I have Windows PowerShell 2.0, is there something in that I can use? &#8212; ML &nbsp; Hello ML, Microsoft Scripting Guy Ed Wilson [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}