{"id":2440,"date":"2013-12-11T00:01:00","date_gmt":"2013-12-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/11\/use-powershell-to-create-remote-session\/"},"modified":"2013-12-11T00:01:00","modified_gmt":"2013-12-11T00:01:00","slug":"use-powershell-to-create-remote-session","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-remote-session\/","title":{"rendered":"Use PowerShell to Create Remote Session"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell to create a remote management session.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my new book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-Step\/dp\/0735663394\/ref=tmm_pap_title_0?ie=UTF8&amp;qid=1383770622&amp;sr=8-1\" target=\"_blank\">Windows PowerShell 3.0 Step by Step<\/a>, published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4442.Step%20by%20Step%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4442.Step%20by%20Step%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p>For a simple configuration on a single remote machine, entering a remote Windows PowerShell session is the answer. To enter a remote Windows PowerShell session, use the <strong>Enter-PSSession<\/strong> cmdlet to create an interactive remote Windows PowerShell session on a target machine. If you do not supply credentials, the remote session impersonates your current sign-in info.<\/p>\n<p>The following output illustrates connecting to a remote computer named <strong>dc1<\/strong><em>. <\/em>When the connection is established, the Windows PowerShell prompt changes to include the name of the remote system. The <strong>Set-Location<\/strong> (<strong>sl<\/strong> is an alias) cmdlet changes the working directory on the remote system to c:\\. Next, the <strong>Get-WmiObject<\/strong> cmdlet retrieves the BIOS information on the remote system. The <strong>Exit<\/strong><em> <\/em>command exits the remote session, and the Windows PowerShell prompt returns to the default.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; Enter-PSSession -ComputerName dc1<br \/> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; sl c:\\<br \/> [dc1]: PS C:\\&gt; gwmi win32_bios<\/p>\n<p> SMBIOSBIOSVersion : A01<br \/> Manufacturer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Dell Computer Corporation<br \/> Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Default System BIOS<br \/> SerialNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 9HQ1S21<br \/> Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : DELL&nbsp;&nbsp; &#8211; 6<\/p>\n<p> [dc1]: PS C:\\&gt; exit<br \/> PS C:\\&gt;<\/p>\n<p>The good thing is that when using the Windows PowerShell transcript tool via <strong>Start-Transcript<\/strong>, the transcript tool captures output from the remote Windows PowerShell session and output from the local session. Indeed, all commands typed appear in the transcript. The following commands illustrate beginning a transcript, entering a remote Windows PowerShell session, typing a command, exiting the session, and stopping the transcript.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; Start-Transcript<br \/> Transcript started, output file is C:\\Users\\administrator.IAMMRED\\Documents\\PowerShe<br \/> ll_transcript.20120701124414.txt<br \/> PS C:\\&gt; Enter-PSSession -ComputerName dc1<br \/> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; gwmi win32_bios<\/p>\n<p> SMBIOSBIOSVersion : A01<br \/> Manufacturer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Dell Computer Corporation<br \/> Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Default System BIOS<br \/> SerialNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 9HQ1S21<br \/> Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : DELL&nbsp;&nbsp; &#8211; 6<\/p>\n<p> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; exit<br \/> PS C:\\&gt; Stop-Transcript<br \/> Transcript stopped, output file is C:\\Users\\administrator.IAMMRED\\Documents\\PowerShe<br \/> ll_transcript.20120701124414.txt<br \/> PS C:\\&gt;<\/p>\n<p>The following image contains a copy of the transcript from the previous session.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1004.hsg-12-11-13-1.png\"><img decoding=\"async\" title=\"Image of transcript\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1004.hsg-12-11-13-1.png\" alt=\"Image of transcript\" \/><\/a><\/p>\n<p>If you anticipate making multiple connections to a remote system, use the <strong>New-PSSession<\/strong> cmdlet to create a remote Windows PowerShell session. <strong>New-PSSession<\/strong> permits you to store the remote session in a variable, and it provides you with the ability to enter and leave the remote session as often as required, without the additional overhead of creating and destroying remote sessions.<\/p>\n<p>In the commands that follow, a new Windows PowerShell session is created via the <strong>New-PSSession<\/strong> cmdlet. The newly created session is stored in the <strong>$dc1<\/strong> variable. Next, the <strong>Enter-PSSession<\/strong> cmdlet is used to enter the remote session by using the stored session. A command retrieves the remote host name, and the remote session is exited via the <strong>Exit<\/strong><em> <\/em>command. Next, the session is re-entered, and the last process is retrieved. The session is exited once again. Finally, the <strong>Get-PSSession<\/strong> cmdlet retrieves Windows PowerShell sessions on the system, and all sessions are removed via the <strong>Remove-PSSession<\/strong> cmdlet.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $dc1 = New-PSSession -ComputerName dc1 -Credential iammred\\administrator<br \/> PS C:\\&gt; Enter-PSSession $dc1<br \/> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; hostname<br \/> dc1<br \/> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; exit<br \/> PS C:\\&gt; Enter-PSSession $dc1<br \/> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; gps | select -Last 1<\/p>\n<p> Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/> &#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/> &nbsp;&nbsp;&nbsp; 292&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp;&nbsp; 39536&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 50412&nbsp;&nbsp; 158&nbsp;&nbsp;&nbsp;&nbsp; 1.97&nbsp;&nbsp; 2332 wsmprovhost<\/p>\n<p> [dc1]: PS C:\\Users\\Administrator\\Documents&gt; exit<br \/> PS C:\\&gt; Get-PSSession<\/p>\n<p> &nbsp;Id Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ComputerName&nbsp;&nbsp;&nbsp; State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConfigurationName&nbsp;&nbsp;&nbsp;&nbsp; Availability<br \/> &nbsp;&#8212; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;<br \/> &nbsp; 8 Session8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dc1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Opened&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Microsoft.PowerShell&nbsp;&nbsp;&nbsp;&nbsp; Available<\/p>\n<p> PS C:\\&gt; Get-PSSession | Remove-PSSession<br \/> PS C:\\&gt;<\/p>\n<p>Join me tomorrow when I will talk about understanding the <strong>While<\/strong> statement.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to create a remote management session. Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my new book, Windows PowerShell 3.0 Step by Step, published by Microsoft Press. For a simple configuration on a single remote machine, entering a [&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":[51,3,4,45],"class_list":["post-2440","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to create a remote management session. Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my new book, Windows PowerShell 3.0 Step by Step, published by Microsoft Press. For a simple configuration on a single remote machine, entering a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2440","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=2440"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2440\/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=2440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}