Share via

List Active Directoy computers

Михаил Андросов 476 Reputation points
2021-12-26T17:53:38.307+00:00

Hi!
I need to get a list of computers in Active Directory in the form of a table consisting of columns: Name, Operating System, Operating System Version, mark, if the last login date is more than 365 days.I used this command:
get-adcomputer -properties * -filter * | ft Name, OperatingSystem, OperatingSystemVersion, LastLogonDate >c:\Downloads\computers.txt
But in the LastLogonDate field I get the date of the last login. How can I output TRUE instead, for example, if the last login was performed for more than 365 days?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
2021-12-26T18:55:25.137+00:00

Hi @Михаил Андросов ,

maybe this helps to get your requirement done:

$dateTime = (Get-Date).AddDays(-380) # Get Date - x days - for testing  
$threshold = (Get-Date).AddDays(-365) # define threshold for "old" in - x days  
# here we go  
$a = $dateTime |  
    Select-Object Date,@{ Name = 'isOld' ; Expression = { If ($_ -lt $threshold ) {$true} else {$false}} }  
$a  

And here your script ( Not tested by myself !!! ):

$threshold = (Get-Date).AddDays(-365) # define threshold for "old" in - x days  
Get-AdComputer -properties -filter |   
    Select-Object Name,OperatingSystem,OperatingSystemVersion,LastLogonDate,@{ Name = 'isOld' ; Expression = { If ($_.LastLogonDate -lt $threshold ) {$true} else {$false}} } |   
        Out-File -Encoding utf8 -FilePath "C:\Downloads\computers.txt"  

----------

(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Михаил Андросов 476 Reputation points
    2021-12-26T20:15:13.803+00:00

    Thank you so much for your help. Your option worked perfectly.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.