title: How can I set the "Environment" > "Start the following program at logon" property on a local user using powershell?
body: <p><strong>Background:</strong><br>
  I have a server with Windows 2008 R2 installed running as a terminal server session host. I have a long list of local users set-up and configured as remote desktop users. When the users remotely log on using remote desktop connection, a program automatically starts up. When the user closes that program, the session ends. This all works fine if I set it up manually.  </p>
  
  <p><strong>My Question:</strong><br>
  I have written a script to add a list of local users automatically and setup and configure the properties. The problem is that nowhere can I find how to set the "Environment" > "Start the following program at logon" properties. (See image for the properties I want to set)  </p>
  
  <p><a href="https://i.stack.imgur.com/lNRnu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lNRnu.png" alt="enter image description here"></a></p>
  
  <p>A sample portion of my current script is as follow:  </p>
  
  <pre><code>  $computer = "localhost"
    $userName = "aTestUser"
    $objComputer = [ADSI]"WinNT://$computer"
    $objUser = $objComputer.Create('user', $userName)
    $objUser.SetPassword("Password")
    $objUser.PSBase.InvokeSet('Description', "Some description for $userName")
    $objUser.PSBase.InvokeSet('userflags', 512)
    $objUser.PSBase.InvokeSet('passwordExpired', 1)
    $objUser.SetInfo();
  </code></pre>
  
  <p>I also tried this command which doesn't work:  </p>
  
  <pre><code>  $objUser.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\programs\a_test_program.exe")  
  </code></pre>
  
  <p>I have searched on Microsoft's MSDN site and Google and StackOverflow but could not find this specific property.  </p>