If you’ve been looking for a Powershell script to find the most recent computers that have been joined to your Active Directory domain, then you’re in luck.

This Powershell script is super simple and is only a few lines of code long. I’ve also paired it with my Next In Line Computer Name Script. We have a standard naming convention when joining computer objects to the domain: company abbreviations, then append a number.

When this script runs, it will output computers that have been joined to the domain within the last 30 days. You can of course change the number to anything you like.


Powershell Script To Filter by Join Date / When Computer Account Was Created

$Joined = [DateTime]::Today.AddDays(-30)
Get-ADComputer -Filter 'WhenCreated -ge $joined' -Properties whenCreated | Format-Table Name,whenCreated,distinguishedName -Autosize -Wrap

Here’s what the output looks like:

If you have the same naming convention we do, then you could obviously just look at the last joined object and create xxxxxxx745 as the next object. However, if you’d like to take it a step further and have it display a box that visually tells you which computer name to use, then follow this guide. (It’s as simple as creating a text file called number.txt and adding the number of the last computer object you joined to the domain.)

If you’ve set that up, then here is the script you could use instead. On the last line, just append your computer prefix in place of the xxxx’s.

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$NotifyUser = {
    [Microsoft.VisualBasic.Interaction]::MsgBox(
        ($args -join ' '), #Notification
        [Microsoft.VisualBasic.MsgBoxStyle]::Information,
        "Next Available Computer Object" #TitleBar
    )
}

[int](get-content "\\fileshare\IT\Scripts\New Hire-Computer\number.txt") + 1 | out-file "\\fileshare\IT\Scripts\New Hire-Computer\number.txt"
$Value = Get-content "\\fileshare\IT\Scripts\New Hire-Computer\number.txt"
$recently = [DateTime]::Today.AddDays(-30)
Write-Host -BackgroundColor Magenta Computers joined to the domain within last 30 days:
Get-ADComputer -Filter 'WhenCreated -ge $recently' -Properties whenCreated | Format-Table Name,whenCreated -Autosize -Wrap

&$NotifyUser Use Computer Name: xxxxxxx$Value

My Homelab Equipment

Here is some of the gear I use in my Homelab. I highly recommend each of them.

The full list of server components I use can be found on my Equipment List page.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *