Locating which Servers a User is Logged onto

The following code allows you to see which servers a particular user account is logged onto. The code allows you to enter the root path in AD to where your servers are and you would like to search from. It also prompts for the users name. It then goes off and searches the servers for logged on accounts and displays whether or not the user is logged onto each server. The only module required for this is the AD module.

The code (Paste this into a .PS1 script file and edit the area of AD you want to search):
(N.B This need to be run in a Powershell window opened in Admin mode else it wil only return the names of servers its checking, but not actually check them)

Import-Module ActiveDirectory
$user = Read-Host “Enter a user name”
$servers = Get-ADComputer -Filter * -SearchBase “ou=OUName,dc=MyDomain,dc=com” | Select-Object -ExpandProperty Name
ForEach ($server in $servers) {
write-host $server
query user $user /server:$server

Possible improvements for this would be to return the idle/disconnected time for the users session so you can kill off sessions that are not being used. Also, to only return the sessions that are logged on and not report a finding for each server in your AD structure.

I will look at doing this and update the post once it is in place.

Hope this helps

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: