Home > Powershell > Powershell: Get Inactive Computer objects in AD

Powershell: Get Inactive Computer objects in AD

I made a new Powershell script to retrieve all inactive and “non used” computer accounts for a specific organizational unit in Active Directory. You need to use the attribute “lastLogonTimestamp” from AD like you can see in the screenshot below. There is also a “lastlogon” attribute present but this an old attribute which is not replicated among the domain controllers. This attribute is only updated on the domain controller you are currently using therefor we use “lastLogonTimestamp” which is replicated. I have also used the  Quest Active Roles which is free to download.

The script below will display first the inactive computer objects that not have been signed-in in the last 3 Months, afterward are the “never used” computer accounts displayed. Modify the $Days & $OU variable to change the number of inactive days and the specified Organizational unit.

Script:


#Display inactive and "non used" workstations of a specific Organizational Unit
#More info: https://jthys.wordpress.com

cls
$Currentdate = get-date
$Days = 90
$OU = "RSRC.int/Managed Computers"

$inactive = Get-QADComputer -SearchRoot $OU -SizeLimit 0 -IncludedProperties LastLogonTimeStamp | where { $_.LastLogonTimeStamp -ne $null -and ($Currentdate-$_.LastLogonTimeStamp).Days -gt $Days }
$neverused = Get-QADComputer -SearchRoot $OU -SizeLimit 0 -IncludedProperties LastLogonTimeStamp | where { $_.LastLogonTimeStamp -eq $null }

Write-Host "Inactive Workstations:"
$inactive | format-table name, lastlogonTimeStamp -autosize

write-host "Never used Worksations:"
$neverused  | format-table name -autosize

Output:

note: I have changed the variable $days to 5 to generate some output on my test domain controller.

 

Categories: Powershell
  1. Paja
    January 12, 2012 at 2:42 pm

    Thank you, this is what I need. But it doesn’t work in my PC “<b style="color:black;background-color:#99ff99"……….“. So I excluded that strings from lines 7,9,12,13.

  2. May 23, 2013 at 9:24 am

    Hello just wanted to give you a quick heads up.

    The text in your post seem to be running off the screen in Safari.
    I’m not sure if this is a format issue or something to do with browser compatibility but I thought I’d post to let you know.
    The style and design look great though! Hope you get the problem
    resolved soon. Thanks

  3. May 30, 2013 at 3:05 pm

    Very good article. I will be experiencing many of these issues as well.
    .

  4. June 21, 2013 at 4:20 pm

    Excellent. To be sure.

  5. June 23, 2013 at 8:55 pm

    Hey! I know this is kinda off topic but I was wondering which blog platform are you using for this
    site? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

    • Joris
      June 24, 2013 at 8:53 am

      Just wordpress.com site.
      not the wordpress.org which you need to host by yourself or someonelse.

  6. June 25, 2013 at 6:55 am

    This is the perfect site for anybody who would like to understand this topic.
    You understand a whole lot its almost tough to argue with you (not
    that I really would want to…HaHa). You definitely put a
    fresh spin on a subject that has been discussed for decades.
    Great stuff, just wonderful!

  7. July 2, 2013 at 5:28 am

    I like the valuable info you provide in your articles. I will bookmark your weblog and check again
    here regularly. I’m quite sure I’ll learn plenty of
    new stuff right here! Good luck for the next!

  8. July 12, 2013 at 1:34 pm

    Hey, I think your site might be having browser compatibility issues.
    When I look at your blog site in Opera, it looks fine but when
    opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, fantastic blog!

  9. July 17, 2013 at 11:44 pm

    You can definitely see your skills within the article you
    write. The sector hopes for more passionate writers like you who aren’t afraid to say how they believe. All the time go after your heart.

  10. July 18, 2013 at 12:00 am

    Will there be an RSS feed that we may sign up for on
    this site so if you have new content that we could be notified?

  11. July 23, 2013 at 11:04 pm

    Nice post. I was checking constantly this blog and I’m impressed! Very helpful info specially the last part 🙂 I care for such info much. I was looking for this particular information for a very long time. Thank you and best of luck.

  1. May 14, 2013 at 9:29 am

Leave a comment