Powershell Scipt for VMware View / vSphere, Who is logged into which VM?

January 24, 2010 — 6 Comments

Update: Wow my script got a mention on the PowerScripting podcast 🙂 Episode 101 – Matthew Giles from Connect-SMART « PowerScripting Podcast

After doing lots of work with VMware View recently I have decided to try and write a few Powershell scripts to assist with View rollouts / administration. This first small script will update a custom field for each VM with the current logged in user. This helps considerably when your are in the vSphere client with a large amount of VM’s and need to track down the user of a particular VM. Maybe you need to log in to check an issue their having, investigate high CPU usage or just quickly find out which VM is assigned to which users. Obviously this can be done through the View Manager but most people won’t have this open on a daily bases.

connect-viserver yourvc
$vms = get-vm *xp*
ForEach($vm in $vms) 
{$QueryString = Gwmi Win32_ComputerSystem -Comp $vm 
$QueryString = $QueryString.userName 
if ($QueryString -eq $null) {$vm | Set-CustomField -Name “Logged in User” -Value "No One Logged In"} ELSE  {$vm | Set-CustomField -Name “Logged in User” -Value $QueryString}}


The script can either be ran as needed or scheduled to run using Windows Scheduled Tasks, for information on how to do this check out Alan Renouf’s blog post http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

As the script uses WMI to get the information regarding the logged in users, you must ensure you run this as user with sufficient privileges over the machines you are querying.

I am using a wild card in the get-vm command to find all my VM’s that contain xp in the name, this can easily be changed to meet the requirements of your View environment.

If no one is logged in when the script runs the custom field is updated with “No One Logged In”

As ever this is a work in progress and I am sure their are numerous ways it could be improved so I look forward to your suggestions and comments.

Example of how it looks in a list


Single VM View


As ever many thanks to Alan Renouf and his blog www.virtu-al.net for the inspiration.


I have mixed success with this script, sometimes I am getting the logged in user returned correctly other time not. Would be interested to head what anyone else’s experiences. It seems the logged in user isn’t always returned from WMI, this seems strange considering I am using a domain admins account.

6 responses to Powershell Scipt for VMware View / vSphere, Who is logged into which VM?


    I think this works a little better a combination of your script and this script:

    Here it is

    connect-viserver virtualcenter

    Function Get-LoggedOn
    $Comp = $args[0]
    Get-WmiObject -computername $Comp -query “Select logonid from Win32_LogonSession where logontype = 2 or logontype = 10” |
    foreach {Get-WmiObject -computername $Comp -query “Associators of {Win32_LogonSession.LogonId=$($_.logonid)} Where AssocClass=Win32_LoggedOnUser Role=Dependent”}

    $vms = get-vm *xp*

    foreach($vm in $vms)
    $QueryString = Get-LoggedOn $vm | Select-Object -First 1
    $QueryString = $QueryString.FullName
    if ($QueryString -eq $null) {$vm | set-customfield -name “Logged in User” -value “No One Logged In”}ELSE{$vm | set-customfield -name “Logged in User” -value $QueryString}



    Here is another way to get pretty much the same thing – except I pull the data from view, rather than query the os in the vm.


Trackbacks and Pingbacks:

  1. All things Virtual IV « TheSaffaGeek - January 26, 2010

    […] Barry Coombs (@virtualisedreality) has posted a powershell script for VMware view that allows to see who is logged into which of the vm’s in your environment. a very helpful script when you need to wade through loads of servers and aren’t sure who is using which server https://virtualisedreality.wordpress.com/2010/01/24/powershell-scipt-for-vmware-view-vsphere-who-is-l… […]

  2. Episode 101 – Matthew Giles from Connect-SMART « PowerScripting Podcast - February 8, 2010

    […] Who’s logged into which VM? […]

  3. Virtualization Short Take #35 - blog.scottlowe.org - The weblog of an IT pro specializing in virtualization, storage, and servers - February 18, 2010

    […] This PowerShell script will show you the logged-in user for a given VMware View desktop. Handy! […]

  4. It’s that time again – Eric Siebert’s Top 25 Bloggers « VirtualisedReality.com - September 14, 2010

    […] View Powershell Script – Who’s logged into which VM […]

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 )

Facebook photo

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

Connecting to %s