Get logged on user for multiple machines with Poweshell


Does anyone have a script that I can use for this ? I used the script the below to check the the UserProfile time and filtered by today's
date .. but I am getting mixed results .. so not sure if this is accurate .. wondering what every one is using ?

Get-WmiObject -ComputerName (get-content C:\PS\Machines.txt) Win32_UserProfile | % {
  $userinfo = [WMI] ($_.__Path -Replace "Win32_UserProfile", "Win32_SID")
  New-Object PsObject -Property @{
    ComputerName= $_.__Server
    Domain      = $userinfo.ReferencedDomainName
    User        = $userinfo.AccountName
    LastUsed    = $_.ConvertToDatetime($_.LastUseTime)
} | export-csv -NoType C:\PS\test_Data.csv

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MilesLoganAuthor Commented:
I tried this and it does return the logged on user but I cant get it to output the PSComputerName or CSNAme value .. so the csv file can have one column with the user and the other with the corresponding machine name

Get-WmiObject Win32_Process -ComputerName MachineName | Where {$ -eq 'explorer.exe'}| Invoke-WmiMethod -name GetOwner | select user

Open in new window

Try this script; you can pipe the computer names to it, or pass them with the -ComputerName argument.
For machines where an error occurred during the query, it will have a column "Exception" which will show the error message.
	[Parameter(ValueFromPipeline=$True, Position=0)]
	[PSObject[]]$ComputerName = @($ENV:ComputerName)
Begin {
Process {
	$ComputerName |
		ForEach-Object {
			Try {
				If ($_ -is [Microsoft.ActiveDirectory.Management.ADComputer]) {
					$ServerName = $_.Name
				} Else {
					$ServerName = $_.ToString()
				Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" -ComputerName $ServerName -ErrorAction Stop |
					Select-Object -Property `
						@{Name="ServerName"; Expression={$_.PSComputerName}},
						@{Name="UserName"; Expression={$_.GetOwner() | % {"$($_.Domain)\$($_.User)"}}},
						@{Name="LogonDate"; Expression={$_.ConvertToDateTime($_.CreationDate)}},
						@{Name="SessionId"; Expression={[UInt32]$_.SessionId}},
						Exception |
					Group-Object -Property SessionId |
					ForEach-Object {
						$_.Group |
							Sort-Object -Property LogonDate |
							Select-Object -First 1
			} Catch {
				$_ | Select-Object -Property `
					@{Name="ServerName"; Expression={$ServerName}},
					@{Name="Exception"; Expression={$_.Exception.Message}}
End {

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Thanks oBdA ! .. worked realy well
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.