PeetersOnline.nl
Oneliner: Get Logged on Users with Powershell
Check out this oneliner/function! Provide a computer name and it will return the logged on users.
function Get-MyLoggedOnUsers
{
param([string]$Computer)
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique | %{“{0}{1}” -f $_.Antecedent.ToString().Split(‘”‘)[1], $_.Antecedent.ToString().Split(‘”‘)[3]}
}
Hugo
No related posts.
7 Responses to Oneliner: Get Logged on Users with Powershell
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Would you happen to know how to retrieve the users logged on to a domain?
[...] Hugo Peeters has a great function for using WMI to get Logged on Users: http://www.peetersonline.nl/index.php/powershell/oneliner-get-logged-on-users-with-powershell/ [...]
Thanks Hugo. Glad to know somebody else’s synapses are firing
Hugo,
your script is almost working
can only give me the following result:
Laptop425SYSTEM
Laptop425LOCAL SERVICE
Laptop425NETWORK SERVICE
MYDOMAINusername
Laptop425ANONYMOUS LOGON
how to get MYDOMAIN\username only ?
I’ve just made a little change in the script to add the computername.
You can now use it with a server list as well as a single server.
function Get-MyLoggedOnUsers
{
param([Array]$Computer)
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer |
Select __SERVER, Antecedent -Unique |
%{"{0} : {1}\{2}" -f $_.__SERVER, $_.Antecedent.ToString().Split('"')[1], $_.Antecedent.ToString().Split('"')[3]}
}
Get-MyLoggedOnUsers localhost
or
Get-MyLoggedOnUsers Server1
or
$Server_List = ("Server1","Server2","Server3")
Get-MyLoggedOnUsers $Server_List
Yours, combined with
http://learn-powershell.net/2010/11/01/quick-hit-find-currently-logged-on-users/
and
http://forums.techarena.in/software-development/1118294.htm
function get-loggedonusers
{
param([Array]$Computer)
$computers = get-wmiobject Win32_Computersystem -computername $Computer
$report = @()
foreach ($c in $computers) {
$temp = "" | Select Computer, Username
$temp.Computer = $c.name
$temp.Username = $c.username
$report += $temp
}#foreach
$report
}#function
Dump anything else from http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102(v=vs.85).aspx in there if needed.
Thanks for your method.
To output the current username by itself, use:
function Get-LoggedOnUsers {
$Computers = HOSTNAME
$Computers | foreach {$((Get-WmiObject Win32_ComputerSystem | Select UserName | Format-Table -HideTableHeaders | Out-String) -split ‘\\’)[1]}
}
Get-LoggedOnUsers
To output the username as DOMAIN\username, use:
function Get-LoggedOnUsers {
$Computers = HOSTNAME
$Computers | foreach {$(Get-WmiObject Win32_ComputerSystem | Select UserName | Format-Table -HideTableHeaders | Out-String)}
}
Get-LoggedOnUsers
Hope this helps