title: How to do a remote PSSession and run a script in the remote desktop?
body: <p>When I try to connect and run a PowerShell script manually from CMD I can do it, but when I try to do the same in my Java code an error is displayed, my code for the connection is this:</p>
  
  <pre class="lang-java prettyprint-override"><code>String command = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell C:\\scr.ps1";
  
  Process powerShellProcess = Runtime.getRuntime().exec(command);
  
  powerShellProcess.getOutputStream().close();
  String line;
  System.out.println("Standard Output:");
  BufferedReader stdout = new BufferedReader(new InputStreamReader(
      powerShellProcess.getInputStream()));
  while ((line = stdout.readLine()) != null) {
      System.out.println(line);
  }
  stdout.close();
  //En caso de error se obtiene
  System.out.println("Standard Error:");
  BufferedReader stderr = new BufferedReader(new InputStreamReader(
      powerShellProcess.getErrorStream()));
  while ((line = stderr.readLine()) != null) {
      System.out.println(line);
  }
  stderr.close();
  System.out.println("Done");
  </code></pre>
  
  <p>Before I tried to put all the commands in a the command string, but that didn't work, so I decided to put it in a script. The script contains this:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$pass = ConvertTo-SecureString "pass" -AsPlainText -Force
  $cred = New-Object System.management.Automation.PSCredential("user", $pass)
  Enter-PSSession -ComputerName User-Pc -Credential $cred ;
  
  dir
  </code></pre>
  
  <p>but the last command <code>dir</code> is done in mi local desktop not in the remote one, I debug and I don't find any error the script works, the connection was done but I don't understand why the <code>dir</code> command doesn't execute in the remote desktop, somebody have any idea of what is happening?</p>