Show computer name

I need a vbscript that if I put a user name then it would give me the computer number ,which that user is currently logged in domain. Appreciate everyone's help.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, you can't really do least very quickly.....without some pre-preparation steps.  To accomplish what you need, you would have to connect to every computer in your domain (without knowing whether they're on or not) and check the logged in user until you find it.  This could take hours per search.

On the other hand, if you follow the options here:

and set up a logon and logoff script that writes user activity to text files, you can immediately query those text files to find out who logged on where.


mikerigelTech Support AnalystCommented:
You could Google and download NBSTAT.  Command line usage:  nbtstat -a [Machine name]

You could also check out
mikerigelTech Support AnalystCommented:
Oops.  Typo should have been NBTSTAT not nbstat.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

pedram_ir68Author Commented:
It is possible to do NBTSTAT  -a [User name]
Because I neeed to find out who is logged into which PC on the domain,
Tony MassaCommented:
You really can't do that reliably with NBTSTAT (at least I never have).  It's best to run a script against all of the computers in realtime that will send the results to a text file.  You will need to add some error checking.  For one-offs, you can use PSLOGGEDON from the SysInternals PSTools suite.

Sample code:
strComputer = InputBox ("Enter Computer Name","Who's LoggedOn")
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
  ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
  Wscript.Echo "User: " & objComputer.UserName

Open in new window

OK, if you really want to run the check on demand, then this script will read the logged in username from every computer on your domain, whenever you run it.  It will output the computer that a user is logged on to into a text file of the username.  You can then open the text file of the specific user to see which computer they were found on.  I have added DOS output, because it can take a long time.


If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
End If

' Specify a folder to place each users' file to
strServerShare = "\\server\share\userlogons"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8
If Right(strServerShare, 1) <> "\" Then strServerShare = strServerShare & "\"
strAllComputers = strServerShare & "All_Computers.txt"
objShell.Run "cmd /c net view > """ & strAllComputers & """", 0, True
Set objComputers = objFSO.OpenTextFile(strAllComputers, intForReading, False)
While Not objComputers.AtEndOfStream
	strLine = Trim(objComputers.ReadLine)
	If strLine <> "" Then
		If Left(strLine, 2) = "\\" Then
			strComputer = Trim(Mid(Left(strLine, 22), 3))
			If Ping(strComputer) = True Then
				WScript.Echo "Reading user logged on to " & strComputer
				On Error Resume Next
				Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
				Set colComputer = objWMIService.ExecQuery("Select UserName From Win32_ComputerSystem")
				strUsername = ""
				For Each objComputer In colComputer
					strUsername = objComputer.UserName
				Set objWMIService = Nothing
				On Error GoTo 0
				If strUsername <> "" Then
					If InStrRev(strUsername, "\") > 0 Then strUsername = Mid(strUsername, InStrRev(strUsername, "\") + 1)
					WScript.Echo "Writing " & strServerShare & strUsername & ".txt"
					Set objOutput = objFSO.OpenTextFile(strServerShare & strUsername & ".txt", intForAppending, True)
					objOutput.WriteLine Now & " - " & strComputer
				End If
				WScript.Echo strComputer & " offline."
			End If
		End If
	End If
objFSO.DeleteFile strAllComputers, True
MsgBox "Done. Please see user files in " & strServerShare

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
		Ping = False
	End If
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pedram_ir68Author Commented:
Thanks Rob for your reply. I ran the script for 4 hours but it didnt generate any text file that includes whose logged on to which PC. It didnt give ant error too.
Do you see a command prompt that shows you that it is connecting to each computer?  How far does it get?
pedram_ir68Author Commented:
the command prompt appears for 10 seconds and then it goes away. please see the attachment.
OK, change this line:
    strCommand = "%comspec% /c cscript  """ & strPath & """"

to this
    strCommand = "%comspec% /k cscript  """ & strPath & """"

Also, do me a favour.  Go to a command prompt, and run
net view > All_Computers.txt

When that's finished, open All_Computers.txt and see what's in it.  It should be a list of computers on your network.

pedram_ir68Author Commented:
exacly what I was looking for.
Great. Thanks for the grade.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.