How can I view via NetSessionEnum() all the current active sessions on a Domain Controller?

Posted on 2011-05-03
Last Modified: 2012-05-11
Folks -

I'd like a simple script I can run that uses the Win32 NetSessionEnum() call to enumerate all the active sessions on a particular Domain Controller.  I'm interested in seeing the computers and the usernames for each computer that have an active session.  This call should provide this information.

I'd prefer a simple VBScript but a Powershell script is fine too.  The script would be run locally on the machine with full administrative rights.  No need for remote connectivity.

If there's a better way to see this information, please let me know.
Question by:amendala
    LVL 6

    Accepted Solution

    VB.NET example
    <DllImportAttribute("Netapi32.dll", EntryPoint:="NetSessionEnum", SetLastError:=True)> _
    Public Shared Function NetSessionEnum(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, _
    <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal UncClientName As String, _
    <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal username As String, _
    ByVal level As UInteger, <OutAttribute()> ByRef bufptr As IntPtr, ByVal prefmaxlen As Integer, _
    <OutAttribute()> ByRef entriesread As Integer, <OutAttribute()> ByRef totalentries As Integer, _
    <InAttribute(), OutAttribute()> ByRef resume_handle As IntPtr) As NET_API_STATUS
    End Function

    refer to

    LVL 70

    Assisted Solution

    by:Chris Dent
    Hmm yeah, that's a long way from being simple in PS, and not even possible in VbScript (unless someone wrote a wrapper to help VbScript out). You'd have to convert the samples here into PowerShell to make headway:

    It's entirely possible, but not something I have time to do until Thursday (this is here in case anyone else wants to). Even then, it'll be less work to use Add-Type in PS and keep the majority of it as C# / VB .NET (rather than PS itself).

    If anyone happens to need an example of that kind of thing, here's one I made for DHCP:

    And if you don't get a response, ping me on Thursday :)

    LVL 6

    Expert Comment


    Author Closing Comment

    I'd forgotten about the PSTool PSLoggedOn, that helped immensely.

    I also whipped up a C# app to implement the call per some of the documentation above.  Both applications return slightly different results but that's due to the manner in which they obtain the information.  PSLoggedOn does not rely exclusively on NetSessionEnum().

    Unfortunately, I cannot share the code I wrote due to workplace IP restrictions.  Sorry.

    Thanks for the help.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    Synchronize a new Active Directory domain with an existing Office 365 tenant
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now