Get-UserSession.psm1

function Get-UserSession {
    <#
.SYNOPSIS
    List User Session from local or remote computer. List with Active Directory SearchBase.
.DESCRIPTION
    List User session from local or remote computer. List with Active Directory SearchBase.
.PARAMETER ComputerName
    Name of server to list User Session from remote computer.
.PARAMETER ADSearchBase
    Active Directory SearchBase of server to list User Session from remote computer.
.EXAMPLE
    Get-UserSession
    List User Sessions from local computer.
.EXAMPLE
    Get-UserSession -ComputerName pc1, pc2
    List User Sessions on multiple remote computer.
.EXAMPLE
    Get-UserSession -SearchBase "OU=Computers,DC=comodo,DC=com".
    List User Sessions on Active Directory SearchBase computer.
#>

    [CmdletBinding(DefaultParameterSetName = "Par1")]
    param (
        [parameter(Position = 0, ParameterSetName = "Par1")] [string[]] $ComputerName = "localhost",
        [parameter(ParameterSetName = "Par2")] [string] $ADSearchBase
    )
    if ($PsVersionTable.OS -match "Windows") {
        if ($ADSearchBase) {
            try {
                $ADSearch = New-Object System.DirectoryServices.DirectorySearcher
                $ADSearch.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$ADSearchBase")
                $ADSearch.Filter = "(&(objectClass=computer)(objectCategory=computer)(!useraccountcontrol:1.2.840.113556.1.4.803:=2))"
                $ADSearchFind = $ADSearch.FindAll()
            }
            catch { Write-Warning "ADSearchBase not found" }
            $ComputerName = $ADSearchFind | ForEach-Object -Parallel { $_.Properties.name } | Sort-Object
        }
        if ($ComputerName) {
            $ComputerName | ForEach-Object -Parallel {
                $Computer = $_.ToUpper()
                if (($Computer -like "localhost") -or ((Test-Connection -Count 1 -TargetName $Computer).Status -match "Success")) {
                    $Quser = quser /SERVER:$Computer 2>&1
                    if (($Quser).Count -gt 1) {
                        $QuserSelect = ($Quser | Select-Object -Skip 1 ).TrimStart().Replace(">", "")
                        foreach ($QuserItem in $QuserSelect) {
                            $Split = $QuserItem.Substring(22) -split "\s{2,}"
                            [PSCustomObject]@{
                                PSTypeName   = "Get-UserSession"
                                ComputerName = $Computer
                                UserName     = $QuserItem.Substring(0, 21).Trim()
                                SessionName  = $Split[0]
                                ID           = $Split[1]
                                Status       = $Split[2]
                                IdleTime     = $Split[3]
                                SessionTime  = $Split[4]
                            }
                        }
                    }
                    else {
                        [PSCustomObject]@{
                            PSTypeName   = "Get-UserSession"
                            ComputerName = $Computer
                            UserName     = $null
                            SessionName  = $null
                            ID           = $null
                            Status       = $null
                            IdleTime     = $null
                            SessionTime  = $null
                        }
                    }
                }
            }
        }
    }
    else { Write-Warning "PSVersion OS not Windows" }
}
New-Alias -Name gus -Value Get-UserSession
Export-ModuleMember -Alias * -Function Get-UserSession