Public/Get-CurrentUserName.ps1
function Get-CurrentUserName { <# .DESCRIPTION Search for an active user login and return the name of the user .EXAMPLE $User = Get-CurrentUserName .NOTES Created by: Jon Anderson Modified: 2023-07-03 #> Write-LogEntry -Value "Checking for an active user login" -Severity 1 $User = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName if([String]::IsNullOrEmpty($User)) { Write-LogEntry -Value "No logged in user found" -Severity 1 return $null } else { $UserName = $User.Split('\') if([String]::IsNullOrEmpty($UserName[1])) { Write-LogEntry -Value "No logged in user found" -Severity 1 return $null } else { Write-LogEntry -Value "Found active user login: $($UserName[1])" return $UserName[1] } } } |