We have already reported on special Active Directory evaluations in the past. Today we would like to show you how you can quickly and easily generate a list using PowerShell , which shows you which user accounts have logged on to AD in the last “x” days .
The period you set for the last login to AD can be chosen freely. So you can very easily and quickly determine which user accounts have carried out an active network registration in the last few days .
The corresponding PowerShell command is as follows.
$ Date = (Get-Date) .AddDays (-1)
Get-ADUser -Filter {LastLogonDate -gt $ Date} | sort name | Select name, distinguishedName
This small PowerShell script first determines in the first line how many days the last login should be traced. In our case it is 1 day, you can of course also adjust it according to your wishes.
The evaluation then looks like this.
The second PowerShell line then examines the last logon date of the users and only shows the users whose last login date is within the selected period. Then we added the sorting of the output by ” Name “, which makes the evaluation of the usernames a little easier.
The final fields are the output fields, in our case ” Name ” and ” DistinguishedName “. Of course you can also change this.
If you prefer to redirect the output to a text file , please add the following to the command.
> DRIVE: PATH FILENAME.TXT
Then a text file is created in the specified path, which you can then edit with Notepad, Excel or another program.
As we mentioned above, we have reported on PowerShell commands in connection with Active Directory in the past . Here is a selection of the most popular articles.
– Find LastLogon (last login) of a user in AD using Get-ADUser
– Edit local users and groups with PowerShell – Query
all user accounts information of a local user – List AD user
accounts that have entered a profile path
– List of all AD users including E- Output email address
– List all deactivated AD accounts
using PowerShell
– Query members of an AD group using PowerShell – Count the number of AD (Active Directory) objects using PowerShell