How to identify list of inactive computers and move them to another OU

New task for IT: identity list of inactive computers and move them to another OU.

As usual, PowerShell will help us.

# Name : ListinActiveComputers.ps1
# Purpose: Get active computer accounts from active directory by
# checking the last logon date. Get the propoerties of computer
# account (name,OS,OSverion,lastlogondate and CanonicalName)
# and save it to inActiveComputers.html file.
#
# Written by Nick Korkishko and Andrew Svintsitsky
# Date written: 05/01/2017
#

Import-Module ActiveDirectory
# Inactive computers Organization unit – where do we need to move inactive computers
$OUname = “OU=InActive computers,DC=nicoljako,DC=local”
# get today’s date
$today = Get-Date

#Get today – 60 days (2 month old)
$cutoffdate = $today.AddDays(-45)

#Get the computer accounts filtered by lastlogondate. Select
#only required properites of the computer account and
$inactivecomputers = Get-ADComputer -Properties * -Filter {LastLogonDate -le $cutoffdate }
#Move Computers to Another OU
$Movedcomputer=@()
foreach ($Computer in $inactivecomputers){
if ($Computer.distinguishedname -notlike “*$OUname*”){
$Computer | Move-ADObject -TargetPath $OUname
$Movedcomputer+=$Computer
}
}
#Export report to HTML
$Movedcomputer | sort -Property LastLogonDate | `
Select Name,OperatingSystem,OperatingSystemVersion,LastLogonDate,CanonicalName | `
ConvertTo-Html -Head “Moved inactive computers to $OUname”| out-file ./inActiveComputers.html

In the end, you will have:

  1. Report about moved inactive computers to proper OU (in this ex. OU=InActive computers,DC=nicoljako,DC=local)
  2. Moved computers to OU=InActive computers,DC=nicoljako,DC=local
Advertisements

About nicoljako

Cofounder Kresalo., sysadmin, architect of IT infrastructures
This entry was posted in Windows Server and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s