title: powershell script to kill .exe terminal server disconnected session
body: <p>I'm looking for a method to kill a .exe of disconnected terminal session. It's about Navision; We have limited licences and want to keep killing the ones that aren't in use.</p>
  
  <p>I know there are methods to completely logoff the disconnected session, but I prefer to only close this specific progress.</p>
  
  <p>I did find a blogpost with a code that is discribed to do exactly what i'm looking for. A powershell script that's sheduled to scan for a certain .exe on disconnected sessions, and kill it off. <a href="http://www.interworks.com/blogs/jpoehls/2012/12/12/killing-processes-disconnected-terminal-service-sessions" rel="nofollow">http://www.interworks.com/blogs/jpoehls/2012/12/12/killing-processes-disconnected-terminal-service-sessions</a> the code is posted on this webpage.</p>
  
  <p>Unfortunately it doesn't work, I get the following error:</p>
  
  <pre><code>PS C:\scripts&gt; .\navscript.ps1
  An empty pipe element is not allowed.
  At C:\scripts\navscript.ps1:91 char:10
  +         | &lt;&lt;&lt;&lt;  % { $id = $_;
  + CategoryInfo          : ParserError: (:) [
  + FullyQualifiedErrorId : EmptyPipeElement
  </code></pre>
  
  <p>Besides alot of time spent on google to find different solutions, I also tried to remove the empty line at line 90. this resulted in another error:</p>
  
  <pre><code>PS C:\scripts&gt; .\navscript.ps1
  Exception calling "Substring" with "2" argument(s): "Lengte kan niet minder dan nul zijn.
  Parameternaam: length"
  At C:\scripts\navscript.ps1:52 char:44
  +                     ;"ID" = $line.Substring &lt;&lt;&lt;&lt; ($line.IndexOf(" ",     $starters.Username), $starters.ID - $line.IndexO
  f(" ", $starters.Username) + 2).trim()
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>I hope someone has an idea how to get this working.</p>