title: Disconnect RDS User Based On Username Input Variable
body: <p>I am trying to create a script that can force logoff of users in an RDS server farm environment. There are 1 of 4 RDS servers that users can be be logged into, and I am trying to make this so you run the script, enter a username and it will force logoff of the user. So far I have:</p>
  
  <pre><code>import-module remotedesktop
  $user = read-host -prompt 'Enter Username you want to disconnect'
  $sid = Get-RDUserSession | Where-Object -filter {$_.UserName -eq $user} | format-wide -Property UnifiedSessionID
  $server = Get-RDUserSession | Where-Object -filter {$_.UserName -eq $user} | format-wide -Property HostServer
  Invoke-RDUserLogoff -HostServer "$server" -UnifiedSessionID $sid
  </code></pre>
  
  <p>The only problem with this in the last line it doesn't like the <code>$sid</code> variable as the input for that item. If I do a <code>write-output $sid</code> it displays the correct <code>UnifiedSessionID</code> number.</p>
  
  <p>I could be doing this the wrong way altogether, but I just need a way to disconnect user sessions based on a username input when prompted.</p>