title: The term is not recognized as a cmdlet, function, operable program, or script file
body: <p>I am trying to execute powershell commands on remote machine using the following code:</p>
  <pre><code>        string shellUri = &quot;http://schemas.microsoft.com/powershell/Microsoft.PowerShell&quot;;
          PSCredential remoteCredential = new PSCredential(&quot;User&quot;, StringToSecureString(&quot;password&quot;));
  
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
              false, &quot;ipaddress&quot;, 5985, &quot;/wsman&quot;, shellUri, remoteCredential, 1 * 60 * 1000);
  
          Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
          runspace.Open();
  
          using (PowerShell ps = PowerShell.Create())
          {
              ps.Runspace = runspace;
              ps.AddCommand(&quot;get-process&quot;);
  
              Collection&lt;PSObject&gt; res = ps.Invoke();
          }
  </code></pre>
  <p>StringToSecureString method listing:</p>
  <pre><code>    private SecureString StringToSecureString(string str)
      {
          SecureString secureStr = new SecureString();
  
          foreach(char c in str)
          {
              secureStr.AppendChar(c);
          }
  
          return secureStr;
      }
  </code></pre>
  <p>I can succesfully execute some commands like &quot;get-process&quot; or &quot;get-service&quot;.</p>
  <p>But there some commands (e.g. &quot;$env:username&quot;) that throw the next error:</p>
  <p>The term is not recognized as a cmdlet, function, operable program, or script file.</p>
  <p>But &quot;$env:username&quot; command will succesfully execute if I connect using powershell directly:</p>
  <pre><code>Enter-PSSession -ComputerName ipaddress -Credential username
  </code></pre>
  <p>There are some other commands not connected to environment variables I can't execute too (CLI app command not working using code nor powershell).</p>
  <p>I am new to powershell and WinRM and have no idea what is wrong. Does anyone know the reason?</p>