Hi Gang,

Any ideas what I might be doing wrong here?

Target it to find all NON Server AD Computer objects with LastLogonTimeStamp defined in $time

Get-ADComputer -Filter {(('OperatingSystem -NotLike "*Server*"') -and (LastLogonTimeStamp -lt $time))} -Properties * | Select -Property Name,operatingSystem,@{Name="LastLogon";Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}}

Thanks, much appreciated.

4 Spice ups

I guess filter is wrong here

Get-ADComputer -Filter {OperatingSystem -notLike “*Server*” -and LastLogonTimeStamp -lt $time} -Properties operatingSystem,lastLogontimeStamp | 
Select  Name,operatingSystem,@{Name="LastLogon";Expression={[DateTime]::FromFileTime($_.lastLogontimeStamp).ToString()}}

get-ADcomputer -filter * -prop OperatingSystem,lastLogontimeStamp|where{$_.OperatingSystem  -notLike “*Server*” -and $_.LastLogonTimeStamp -lt $time}|
Select -Property Name,operatingSystem,@{Name="LastLogon";Expression={[DateTime]::FromFileTime($_.lastLogontimestamp).ToString()}}

How did you define $time ? What errors are you getting? Heres what I use to find InActive Computer Objects:

import-module activedirectory  

#change OU if needed, use Distinguished Name
$domain = "Put your OU Distinguished Name here"  

#change to query how many days since
$DaysInactive = 45  

$time = (Get-Date).Adddays(-($DaysInactive)) 
  
# Get all AD computers with lastLogonTimestamp less than our time 
Get-ADComputer -Searchbase $domain -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | 
  
# Output hostname and lastLogonTimestamp into CSV, change save location if needed 
select-object Name,@{Name="Last Logon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv e:\scripts\Inactive_Computers45Day.csv -notypeinformation

Spot on @JitenSh

This works now.

Import-Module ActiveDirectory
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
Get-ADComputer -Filter {(OperatingSystem -notlike "*windows*server*") -and (OperatingSystem -notlike "*Linux") -and (OperatingSystem -notlike "*Red*Hat*") -and (OperatingSystem -notlike "*unknown*") -and (LastLogonTimeStamp -lt $time)} -Properties * | Select -Property Name,operatingSystem,@{Name="LastLogon";Expression={[DateTime]::FromFileTime($_.lastLogontimestamp).ToString()}}

Hi Guys,

Wanted to revisit this script again.

Import-Module ActiveDirectory
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
$computers = Get-ADComputer -Filter {(OperatingSystem -notlike "*windows*server*") -and (OperatingSystem -notlike "*Linux") -and (OperatingSystem -notlike "*Red*Hat*") -and (OperatingSystem -notlike "*unknown*") -and (LastLogonTimeStamp -lt $time)} -Properties *
foreach ($computer in $computers) {
Remove-ADComputer $computer.name }

I wanted to get a peer review on this script to confirm if this can be used as a scheduled task to run every weekend to safely remove stale NON Server AD Computer objects.

Thanks. Appreciate all the help.

@jitensh