Getting currently logged on user

Welcome Forums General PowerShell Q&A Getting currently logged on user

Viewing 3 reply threads
  • Author
    • #254651
      Topics: 3
      Replies: 4
      Points: 54
      Rank: Member


      I hope this is the right place to write my question.

      I want to search all my domain computers for the currently logged on user.

      I have this command:

      Get-WMIObject -Class Win32_ComputerSystem -ComputerName (Get-Content C:\Temp\ComputerNames.txt) | Select Name, Username

      It works great, but I want to it to skip computers that don’t reply, turned off or out of the office (in the case of laptops).

      How would I do that?

      Thank you,

    • #254669
      Topics: 17
      Replies: 1958
      Points: 4,020
      Helping Hand
      Rank: Community Hero

      There are many scripts for getting logged on users. Have you tried to search “Powershell Get Logged On User”? If you want to write it yourself, you can minimally use the scripts as a guide as they will have logic to test connections.

    • #254675
      Topics: 1
      Replies: 97
      Points: 446
      Helping Hand
      Rank: Contributor

      Check out Test-Connection.

      You could do something like the following. However, this will be slow for a lot of computers.

      This type of task is better suited for a configuration/inventory management solution.

    • #255161
      Topics: 27
      Replies: 28
      Points: 250
      Rank: Participant

      Please try the below code:

      nRDP Session List - " + $Today + " nn" $CurrentSN = 0 $Servers = Get-Content C:\Temp\ComputerNames.txt $NumberOfServers = $Servers.Count # Iterate through the retrieved list to check RDP sessions on each machine ForEach ($Server in $Servers) { Write-Host "Processing $Server ..." -ForegroundColor Yellow Write-progress -activity "Checking RDP Sessions" -status "Querying $Server" -percentcomplete (($CurrentSN / $NumberOfServers) * 100) try { $SessionList += qwinsta /server:$Server | Select-Object -Skip 1 | % { [PSCustomObject] @{ Type = $_.Substring(1, 18).Trim() User = $_.Substring(19, 20).Trim() ID = $_.Substring(41, 5).Trim() State = $_.Substring(48, 6).Trim() } } | ? { $_.Type -notin 'console', 'services', 'rdp-tcp' -and $_.User -ne $null -and $_.User -ne 65536 } | % { [PSCustomObject] @{ Server = $Server User = $_.User LogonType = $_.Type ID = $_.ID State = $_.State } "n$Server logged in by $($_.User) on $($_.Type), session id $($_.ID) $($_.state)" } } catch { $SessionList += "n Unable to query " + $Server write-host "Unable to query $Server! `n $($Error[0].Exception)" -foregroundcolor Red } $CurrentSN++ } # Send the output the screen. $SessionList + " nn" $sendMailArgs = @{ From = "$env:[email protected]$env:userdnsdomain" To = '[email protected]' SmtpServer = '' Priority = 'High' Body = $SessionList | Select-Object Server, User, LogonType, ID, State Subject = "$($SessionList.Count) Logged On users from $($NumberOfServers) online servers as at $($Today)" } Send-MailMessage @sendMailArgs[/crayon]


      Hope that helps.

      • This reply was modified 4 months, 3 weeks ago by IT Engineer.
      • This reply was modified 4 months, 2 weeks ago by grokkit.
Viewing 3 reply threads
  • The topic ‘Getting currently logged on user’ is closed to new replies.