title: Execute powershell script remotely on Amazon EC2 instance from my local computer
body: <p>I have an Amazon EC2 instance.</p>
  
  <p>Using powershell on my local workstation, I want to be able to remote into my Amazon EC2 instance and execute some commands.</p>
  
  <p>I have found many articles online but none are working or I misunderstood them (probably the latter).</p>
  
  <p>Some I tried are<br>
  <a href="https://blogs.iis.net/thomad/archive/2010/08/12/managing-windows-amazon-ec2-machines-remotely-with-powershell.aspx" rel="nofollow noreferrer">Managing Windows EC2 Instances remotely with Powershell</a><br>
  <a href="https://serverfault.com/questions/191723/administering-ec2-instance-with-windows-powershell">Administering EC2 instance with Windows Powershell</a><br>
  <a href="http://powershell.com/cs/forums/t/9879.aspx" rel="nofollow noreferrer">Enabling- PSRemoting</a><br>
  <a href="http://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/" rel="nofollow noreferrer">How to Run PowerShell Commands on Remote Computers</a>  </p>
  
  <p>My understanding is that I need to:  </p>
  
  <pre><code>Amazon EC2 Dashboard &gt; Network &amp; Security &gt; Security Groups &gt; Add port 5985
  
  //Local &amp; EC2 PowerShell(Administrator)
  enable-psremoting -force
  
  //Local PowerShell(Administrator)
  set-item wsman:\localhost\Client\TrustedHosts -value "*" -force
  
  $password = convertto-securestring -asplaintext -force -string myPassword
  $credential = new-object -typename system.management.automation.pscredential -argumentlist "myUsername", $password
  $session = new-pssession ec2-00-00-00-000.compute-1.amazonaws.com -credential $credential
  enter-pssession $session  
  </code></pre>
  
  <p>But I get this error  </p>
  
  <pre><code>new-pssession : [ec2-00-00-00-000.compute-1.amazonaws.com] Connecting to remote server
  ec2-00-00-00-000.compute-1.amazonaws.com failed with the following error message : WinRM cannot complete the
  operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and
  that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM
  firewall exception for public profiles limits access to remote computers within the same local subnet. For more
  information, see the about_Remote_Troubleshooting Help topic.
  At line:1 char:12
  + $session = new-pssession ec2-00-00-00-000.compute-1.amazonaws.com -credential $c ...
  +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
     gTransportException
      + FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionOpenFailed
  </code></pre>