# Login History Domain Member Server

**URL:** <https://forums.powershell.org/t/login-history-domain-member-server/20690>\
**Category:** PowerShell Help\
**Created:** [October 19, 2022, 9:29am UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690 "2022-10-19T09:29:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Powernoob](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@Powernoob](https://forums.powershell.org/u/Powernoob)\
**Post date:** [October 19, 2022, 9:29am UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/1 "2022-10-19T09:29:43Z")

</div>

Hello,  
first of all I am a complete Powershell beginner.  
And have found only approaches to my concern but no concrete solution.  
I would like to display the login history (successful logins) of the last 2 months for all users on a certain domain member server. Failed logins should not appear.

I would be very happy about a solution and it would also be a great learning effect for me.  
Many thanks in advance  
Best greetings

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2022, 10:15am UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/2 "2022-10-19T10:15:22Z")

</div>

Powernoob,  
Welcome to the forum. 👋🏽

> [@Powernoob](#):
>
> … login history (successful logins) of the last 2 months for all users on a certain domain member server.

My first idea would be to parse the according eventlogs for the related events. But …

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

---

<div class="post-metadata">

**Author:** ![Powernoob](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@Powernoob](https://forums.powershell.org/u/Powernoob)\
**Post date:** [October 19, 2022, 12:35pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/3 "2022-10-19T12:35:11Z")

</div>

Here is my first draft.  
But how to use it to query the domain with username and login history. No idea.  
Get-EventLog System -Source Microsoft-Windows-WinLogon -After (Get-Date).AddDays(-180) -ComputerName $env:computername \> C:\Logfiles\Test\An\_Abmeldelogs.txt

… I would run the script on the member server in question. Or does it have to run on the DC?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 19, 2022, 3:58pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/4 "2022-10-19T15:58:46Z")

</div>

To the best of my knowledge, the log you have chosen will not get you what you want. You would need to parse the Security log for specific events. This is what I use. Since your original code specifies a specific log on a specific system, this does the same:

```auto
$SuccessLoginsFilter = "(Event[System[EventID=4624 or EventID=4648]]) and (Event[EventData[Data[@Name='LogonType'] = '2']] or Event[EventData[Data[@Name='LogonType'] = '3']] or Event[EventData[Data[@Name='LogonType'] = '7']] or Event[EventData[Data[@Name='LogonType'] = '10']] or Event[EventData[Data[@Name='LogonType'] = '11']]) and (Event[EventData[Data[@Name='SubjectUserSid'] != 'S-1-0-0']])"

Get-WinEvent -Path 'C:\Windows\System32\winevt\logs\security.evtx' -FilterXPath $SuccessLoginsFilter | Where-Object {$_.TimeCreated -ge (Get-Date).AddDays(-180)}

```

Hopefully, this will get you started. You could also get tons of examples via a google search.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2022, 5:04pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/5 "2022-10-19T17:04:31Z")

</div>

Since I actually started this answer before @tonyd and just got distracted with work I will post it anyway. Especially because it shows a different approach.

And since you’re a beginner you probably wouldn’t learn much when I simply provide the working code … 😉 … so let’s do it step by step.

First of all … when you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://aws1.discourse-cdn.com/business5/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

Second … don’t use `Get-Eventlog` anymore. Instead use

> **[Get-WinEvent (Microsoft.PowerShell.Diagnostics) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.diagnostics/get-winevent?view=powershell-7.2)**
>
> This cmdlet is only available on the Windows platform. The Get-WinEvent cmdlet gets events from event logs, including classic logs, such as the System and Application logs. The cmdlet gets data from event logs that are generated by the Windows Event...

I’d receommend to use a filter hashtable since it is easy to read and maintain. I’m unsure if you picked the right eventlog but let’s use it for now for the first steps just to show how it may work:

```auto
$FilterHashtable = @{
    logname = 'Microsoft-Windows-Winlogon/Operational'
    StartTime = (Get-Date).Date.AddDays(-180)
    EndTime = (Get-Date).Date
}

```

Now we query the eventlog and limit the output to maybe 20 for now … just to see what we will get and be able to pick what’s useful for us …

```auto
Get-WinEvent -FilterHashtable $FilterHashtable -MaxEvents 20 | 
    Select-Object -Property *

```

Now you should inspect the output and figure out what properties you need. It may be enough to get events with the Id of 812 since we don’t need two return values per event … I guess. So we modify our hashtable like this:

```auto
$FilterHashtable = @{
    logname = 'Microsoft-Windows-Winlogon/Operational'
    StartTime = (Get-Date).Date.AddDays(-180)
    EndTime = (Get-Date).Date
    Id = 812
}

```

Running the query again I noticed that we have some local accounts as well. I’d assume you don’t need them, right? So we get rid of them with a

> **[Where-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.2)**
>
> The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a...

```auto
Get-WinEvent -FilterHashtable $FilterHashtable -MaxEvents 20 | 
    Where-Object {$_.UserId.ToString().length -gt 8} |
        Select-Object -Property UserId, TimeCreated

```

Now you just need to translate the SIDs into names. In order not to have to query the AD again and again, you should do this beforehand. And to easily translate the SID into the sAMAccountName we can use a hashtable.

```auto
$UserHashtable = @{}
$SearchBase = 'OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=com'
Get-ADUser -Filter "enabled -eq 'true'" -SearchBase $SearchBase |
    ForEach-Object {
        $UserHashtable.add($_.SID.Value, $_.sAMAccountName)
    }

```

Now we can use this hashtable in a calculated property …

```auto
Get-WinEvent -FilterHashtable $FilterHashtable | 
    Where-Object {$_.UserId.ToString().length -gt 8} |
        Select-Object -Property UserId, TimeCreated, 
                @{Name = 'UserName'; Expression = {$UserHashtable[$_.UserId.Value]}}

```

This should give you an idea of how to approach a task like this.

A general tip at the end: You should always read the help for the cmdlets you’re about to use COMPLETELY INCLUDING THE EXAMPLES to learn how to use them. 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 19, 2022, 5:25pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/6 "2022-10-19T17:25:34Z")

</div>

Thanks Olaf, sorry for butting in ☹

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2022, 5:26pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/7 "2022-10-19T17:26:47Z")

</div>

No need to sorry at all. That’s we’re all here for, isn’t it? 😉

---

<div class="post-metadata">

**Author:** ![Powernoob](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@Powernoob](https://forums.powershell.org/u/Powernoob)\
**Post date:** [October 20, 2022, 9:31am UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/8 "2022-10-20T09:31:20Z")

</div>

Thank you very much for the very helpful answers. 👍 👍 👍  
I will try to implement it accordingly.  
In any case, very helpful for me as a beginner and an important foundation to further expand my knowledge.  
Kind regards

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/login-history-domain-member-server/20690/9 "2024-05-16T20:16:29Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


