Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Last active June 8, 2018 14:09
Show Gist options
  • Save alexinnes/4157865c0b0c1aaf4f6aec22055fe26c to your computer and use it in GitHub Desktop.
Save alexinnes/4157865c0b0c1aaf4f6aec22055fe26c to your computer and use it in GitHub Desktop.
Gets the user object of a user that is logged into a station.
function Get-LoggedOnUser {
<#
.Synopsis
Gets the logged in user of the station
.DESCRIPTION
Uses WMI to get the user who is logged in while stripping out the common SIDS
.EXAMPLE
Get-LoggedOnUser
.EXAMPLE
Get-LoggedOnUser -ComputerName Some-Computer
.INPUTS
will accept values from the pipeline: "Computer", "__SERVER", "IPAddress"
.OUTPUTS
Will output type: System.Management.ManagementObject#root\cimv2\Win32_UserProfile
.NOTES
General notes
#>
[CmdletBinding()]
Param (
# Specify the name of one or more computers
[Parameter(
Position = 0,
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'Computer'
)]
[Alias("Computer", "__SERVER", "IPAddress")]
$ComputerName = $env:COMPUTERNAME
)
#can filter out common sids that appear
# https://support.microsoft.com/en-gb/help/243330/well-known-security-identifiers-in-windows-operating-systems
$commonSIDS = @(
".DEFAULT",
"S-1-5-18",
"S-1-5-19",
"S-1-5-20",
"_Classes"
)
$wmiLoadedUser = Get-WmiObject win32_userprofile -ComputerName $computerName | where{$_.loaded -eq $true -and $_.SID -notmatch ($commonSIDS -join '|') }
$wmiLoadedUser
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment