{"id":12071,"date":"2011-11-16T00:01:00","date_gmt":"2011-11-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/16\/configure-powershell-remoting-and-use-remote-commands\/"},"modified":"2011-11-16T00:01:00","modified_gmt":"2011-11-16T00:01:00","slug":"configure-powershell-remoting-and-use-remote-commands","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/configure-powershell-remoting-and-use-remote-commands\/","title":{"rendered":"Configure PowerShell Remoting and Use Remote Commands"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to configure Windows PowerShell remoting, store credentials, and use remote commands.<\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-family: Lucida Sans Typewriter;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! I keep hearing about Windows PowerShell remoting. But I do not really know what Windows PowerShell remoting is. For example, I can use the <b>Get-WMIObject<\/b> to work with remote computers. Is that remoting? If so, what is the big deal? I could do that in Windows PowerShell 1.0. Is this just a bunch of typical marketing hype, or is there something that actually works here and I am missing it?<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&mdash;GB<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello GB, <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. GB, I hate to tell you, but you are missing the boat. There is a common misconception about what Windows PowerShell remoting really is. There are several ways to run a command on a remote computer:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Windows Management Instrumentation (WMI) can target a remote computer via the <i>computername <\/i>parameter. The <b>Get-WMiObject<\/b> cmdlet also allows for alternate credentials.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The <i>computername <\/i>cmdlets (not including the <b>Get-WmiObject<\/b> cmdlet). There are a lot of cmdlets that have a <i>computername <\/i>parameter. These cmdlets permit making a connection to a remote computer and retrieving information from them. However, not all of these cmdlets have a <i>credential <\/i>parameter and therefore they must run with administrator rights on the remote computer. In addition, in many cases these cmdlets require specific holes open in the firewall, and even certain services running on the remote machine before they will work properly. To find the cmdlets that have a <i>computername <\/i>parameter, use the following Windows PowerShell command:\n<p>get-command -CommandType cmdlet | where { $_.definition -match &#8216;computername&#8217;}<br \/><\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Some <i>computername <\/i>cmdlets do permit the use of alternative credentials. These cmdlets allow you to run a command against a remote machine, and to specify the context in which to run the command. This solves the problem of supplying alternative credentials. To find these cmdlets, use the following Windows PowerShell command (<b>gcm<\/b> is an alias for the <b>Get-Command<\/b> cmdlet, and <b>?<\/b> is an alias for the <b>Where-Object<\/b> cmdlet; the command is a single command, and I did not include any line continuation;it has wrapped in the output here, but would appear on a single line in the Windows PowerShell console): \n<p>gcm -CommandType cmdlet | ? { $_.definition -match &#8216;computername&#8217; -AND $_.definition -match &#8216;credential&#8217;}<br \/><\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">True, Windows PowerShell remoting is the other way to run a Windows PowerShell command on a remote computer. Windows PowerShell remoting uses <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa384426(v=vs.85).aspx\"><span>Windows Remote Management<\/span><\/a><\/span><span style=\"font-family: Segoe\"> (WinRM) as the underlying technology, and is therefore firewall friendly. WinRM is Microsoft&rsquo;s implementation of the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa384470(v=vs.85).aspx\"><span>WS-Management Protocol<\/span><\/a><\/span><span style=\"font-family: Segoe\"> that allows hardware and operating systems from different vendors to interoperate. It is industry standard, and extremely powerful. <\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The first thing to do is to enable and configure Windows PowerShell remoting. To do this, use the <b>Enable-PSRemoting<\/b> cmdlet. Enable Windows PowerShell remoting on all machines that will communicate. If this only involves a couple computers, using the <b>Enable-PSRemoting<\/b> cmdlet works fine. But if you need to turn on Windows PowerShell remoting on an entire organizational unit, domain, or forest, it is better to use Group Policy. Unfortunately, there are myriad Group Policy settings from which to choose, and I like the convenience of the <b>Enable-PSRemoting<\/b> cmdlet. Therefore, I use a script that calls the <b>Enable-PSRemoting<\/b> cmdlet with Group Policy and assign it as a logon script. I discuss this technique in the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/11\/16\/enable-powershell-remoting-to-enable-running-commands.aspx\"><span>Enable PowerShell Remoting to Enable Running Commands<\/span><\/a><\/span><span style=\"font-family: Segoe\"><i> <\/i>blog post. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After it is configured, I like to use the <b>Invoke-Command<\/b> cmdlet to ensure that Windows PowerShell remoting works properly. By running a simple command such as hostname.exe,<i> <\/i>I ensure that Windows PowerShell remoting works, and confirm the actual location that ran the command. Here is the syntax:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">invoke-command -cn syddc01 -credential contoso\\administrator -scriptblock {hostname}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I have run that command, I know everything works. Now, I like to store my credentials in a variable to make it easier to run remote commands. To do this, I use the <b>Get-Credential<\/b> cmdlet. Here is the command I use:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$cred = Get-credential contoso\\administrator<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When the command runs, the following dialog box is displayed. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4532.HSG-11-16-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of dialog box displayed when command is run\" alt=\"Image of dialog box displayed when command is run\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4532.HSG-11-16-11-1.png\" width=\"423\" height=\"337\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I have a credential object stored in the <b>$cred<\/b> variable, I use it to invoke a remote command. These commands are shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$cred = Get-Credential contoso\\administrator<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">invoke-command -cn syddc01 -cred $cred -script {hostname}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Now, I want to use the <b>Invoke-Command<\/b> cmdlet to run the <b>Get-Process<\/b> cmdlet on a remote computer and use alternate credentials. The syntax for this command is shown here (this command uses the <b>cn<\/b> computername alias for <b>Invoke-Command<\/b>, the credentials stored in the <b>$cred<\/b> variable, and the <b>gps<\/b> alias for the <b>Get-Process<\/b> cmdlet):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Invoke-Command -cn syddc01 -Credential $cred -ScriptBlock {gps}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0726.hsg-11-16-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0726.hsg-11-16-11-2.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">GB, that is all there is to getting Windows PowerShell remoting up and running, and storing credentials in a credential object to simplify running remote commands. Join me tomorrow when I will talk about creating remote Windows PowerShell sessions. <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span>Twitter<\/span><\/a><\/span><span style=\"font-family: Segoe\"> and <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span>Facebook<\/span><\/a><\/span><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span>scripter@microsoft.com<\/span><\/a><\/span><span style=\"font-family: Segoe\">, or post your questions on the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span>Official Scripting Guys Forum<\/span><\/a><\/span><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-size: small\"><b>&nbsp;<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to configure Windows PowerShell remoting, store credentials, and use remote commands. &nbsp; Hey, Scripting Guy! I keep hearing about Windows PowerShell remoting. But I do not really know what Windows PowerShell remoting is. For example, I can use the Get-WMIObject to work with remote computers. Is that remoting? If so, what is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[57,3,4,45],"class_list":["post-12071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-remoting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to configure Windows PowerShell remoting, store credentials, and use remote commands. &nbsp; Hey, Scripting Guy! I keep hearing about Windows PowerShell remoting. But I do not really know what Windows PowerShell remoting is. For example, I can use the Get-WMIObject to work with remote computers. Is that remoting? If so, what is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12071","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12071\/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=12071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}