How To Find Next Available Computer Name in Powershell

by Danny
0 comment

READING TIME: 4 MINUTES

In the IT world, it’s common practice to name all of the computers you join to the domain with the same naming convention. For example, we use company abbreviations-operating system-next in line number. The computers we join to the domain look like hsbw10p611, hsbw10p612, etc.

Every new computer object gets the next incremented number. Typically, whenever we join a new PC to our domain, we first have to open Active Directory Users & Computers (ADUC widget) to find the next one. Or, we keep a shared CSV that we manually update each time a new computer is added.

In my opinion, both of these options are not great solutions. For example, if a computer object gets moved to another OU – we may overlook it when looking in the default “Computers” OU. If using a spreadsheet, we may forget to check off the last used computer name.

So, with the help of some Redditors in this thread, I finally have a simple, effective solution: Powershell.


The Issue

I originally created a script that successfully found “available” computer objects, not the very last one in a list.

For example, if we deleted a computer object hsbw10p419, the script would return that computer name to use. I didn’t want to do this, as we often have IT tickets or emails that reference the old computer name. So, I wanted something that “skips” all computer objects and just spits out the next available one.

Another issue I ran into with my script is it would display computer objects that didn’t match our normal naming convention. For example, we have a few outlier computer objects like hsbw10pdanny. No numbers at the end at all. I then attempted to exclude all of those outliers by hardcoding them into the script. This also wasn’t a great solution, as we may have more exclusions like this in the future and I didn’t want to have to update the script whenever this occurred.


The Solution

So, here’s what I settled on for the solution:

Create a counter script that starts at the last computer object we joined to the domain (hsbw10p700), and save that number to a text file. Every time the script runs, it increments the number in the next file.

This effectively always displays the next computer name we should use (as long as the IT team clicks the button in increment it!)


Create a Number.txt file

To do this, first create a Number.txt file and save it somewhere like C:\Scripts\Final\.

In your text file, add the number of the last computer that was joined to the domain. (In my example, this is 700)


Powershell Script to Find Available Computer Name

Everytime the script is ran, it increments the number in the text file, prepends the our computer name prefix (hsbw10p), and then exports the text file again.

This eliminates the need to guess what computer name to use. The only caveat is that you need to make sure the script is ran each time a computer is added to the domain. The script has no knowledge of AD, so if someone manually joins a computer to the domain without using the script, the the script will output a number that was already used.

[int](get-content C:\Scripts\Final\number.txt) + 1 | out-file C:\Scripts\Final\number.txt
$Value = Get-content C:\Scripts\Final\number.txt 
Write-Host -BackgroundColor DarkGreen Use Computer Name: hsbw10p$Value

I then put this script into my simple Powershell Menu GUI like you see below so it’s easily accessed by the entire IT team.

When the script runs, it looks like this:


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.

0 comment

You may also like

Leave a Comment