code: public PowerShellResponse executeCommand(String command) {
          String commandOutput = "";
          boolean isError = false;
          boolean timeout = false;
  
          checkState();
  
          PowerShellCommandProcessor commandProcessor = new PowerShellCommandProcessor("standard", p.getInputStream(),
                  this.waitPause, this.scriptMode);
          Future<String> result = threadpool.submit(commandProcessor);
  
          // Launch command
          commandWriter.println(command);
  
          try {
              if (!result.isDone()) {
                  try {
                      commandOutput = result.get(maxWait, TimeUnit.MILLISECONDS);
                  } catch (TimeoutException timeoutEx) {
                      timeout = true;
                      isError = true;
                      //Interrupt command after timeout
                      result.cancel(true);
                  }
              }
          } catch (InterruptedException | ExecutionException ex) {
              logger.log(Level.SEVERE,
                      "Unexpected error when processing PowerShell command", ex);
              isError = true;
          } finally {
              // issue #2. Close and cancel processors/threads - Thanks to r4lly
              // for helping me here
              commandProcessor.close();
          }
  
          return new PowerShellResponse(isError, commandOutput, timeout);
      }
docs: Execute a PowerShell command.
  <p>
  This method launch a thread which will be executed in the already created
  PowerShell console context
  
  @param command the command to call. Ex: dir
  @return PowerShellResponse the information returned by powerShell