# Get the next available unused AD ComputerName and OU location?

**URL:** <https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579>\
**Category:** PowerShell Help\
**Created:** [October 29, 2018, 6:40pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579 "2018-10-29T18:40:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [October 29, 2018, 6:40pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/1 "2018-10-29T18:40:58Z")

</div>

People,

I wonder if anyone can assist me in modifying the below PowerShell code that shows the OU where the Computer Object name can be used:

> $patterns = 'PRD', 'DR', 'TEST'

> ForEach( $prefix in $patterns) {
> 
> Get-ADComputer-Filter "Name -like '$($prefix)\*'"|
> 
> ForEach-Object {[int]($\_.Name-replace'(?:.\*?)(\d+)\Z','$1')} |
> 
> Measure-Object-Maximum |
> 
> ForEach-Object {$prefix+ (++$\_.Maximum).ToString('000')}
> 
> }

Because at the moment it is just showing the next available name, and I'd like the expected output to be:

> OU: domain.com/Servers/Production
> 
> Next available computer name: PRDSVR003
> 
> OU: domain.com/Servers/Test
> 
> Next available computer name: TESTSVR027
> 
> OU: domain.com/Servers/DR Site
> 
> Next available computer name: DRSVR089

Without the error:

> Cannot convert value "PRD23-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
> 
> Cannot convert value "TEST72-DEV" to type "System.Int32". Error: "Input string was not in a correct format."

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 29, 2018, 7:21pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/2 "2018-10-29T19:21:58Z")

</div>

You cannot cast a type [int] when the value contains others than numbers.

```
# this will fail
[int]‘nineteen’

# this not

[int]‘19’
```
  
Could you please format your code as code? It will make your life and ours easier. Thanks.

---

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [October 29, 2018, 7:51pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/3 "2018-10-29T19:51:37Z")

</div>

Hi Olaf,

```
$patterns = 'PRD', 'DR', 'TEST'

ForEach( $prefix in $patterns) {
    Get-ADComputer -Filter "Name -like '$($prefix)*'" |
        ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
        Measure-Object -Maximum |
        ForEach-Object {$prefix + (++$_.Maximum).ToString('000')}
}
```

OK, so I have format the code like the above.  
Is it possible to ignore the non-numerical value?

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [October 29, 2018, 8:05pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/4 "2018-10-29T20:05:31Z")

</div>

Is this “[int]($\_.Name” not exactly what Olaf was saying, about not being able to define [int]‘nineteen’, except you’re trying to define [int]'PRD", [int]‘DR’, [int]‘TEST’.

---

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [October 29, 2018, 8:21pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/5 "2018-10-29T20:21:53Z")

</div>

> [@](#):
>
> Is this “[int]($\_.Name” not exactly what Olaf was saying, about not being able to define [int]‘nineteen’, except you’re trying to define [int]'PRD", [int]‘DR’, [int]‘TEST’.

```
$patterns = 'PROD', 'TEST', 'DR'
$CurrentADComputerName = $null
$NextADComputerName = $null

ForEach ( $prefix in $patterns) {
    
    $CurrentADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" | 
        Sort-Object Name | 
        Select-Object -Expand Name | 
        Select-Object -Last 1
    Write-Host "The current AD Computername for $prefix is: $CurrentADComputerName" -BackgroundColor Blue -ForegroundColor Yellow

    $CurrentOULocation = (Get-ADComputer $CurrentADComputerName -Properties *).DistinguishedName

    $NextADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" |
        ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
        Measure-Object -Maximum |
        ForEach-Object {$prefix + (++$_.Maximum).ToString('000')}
    Write-Host "The next available AD Computername for $prefix is: $NextADComputerName" -BackgroundColor Blue -ForegroundColor Green
    Write-Host "Which can be deployed in OU: $CurrentOULocation `n`n" -BackgroundColor Blue -ForegroundColor White

}
```

OK, I have modified it like the above code, but so far I need to suppress the error with the AD object that is like the below:

> Cannot convert value "PRD23-OLD" to type "System.Int32". Error: "Input string was not in a correct format." Cannot convert value "TEST72-DEV" to type "System.Int32". Error: "Input string was not in a correct format."

and how to display the OU location like: **domain.com/SITE 1/Server/Production** not as DN with CN ?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 29, 2018, 8:40pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/6 "2018-10-29T20:40:40Z")

</div>

> [@](#):
>
> Is it possible to ignore the non-numerical value?

No. You need to “extract/separate” the numbers from whatever string you have, cast it to [INT], treat it accordingly and use this for whatever further action you need to do.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 30, 2018, 1:41am UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/7 "2018-10-30T01:41:03Z")

</div>

> [@](#):
>
> OK, I have modified it like the above code, …

No you have not. You still have the “**[int]**” in your code.  
Change your code line 16 from

```
ForEach-Object {[int]($_.Name -replace ‘(?:.\*?)(\d+)\Z’, ‘$1’)} |_
```
 _… to …_ 
```
_ForEach-Object {($_.Name -replace ‘(?:.*?)(\d+)\Z’, ‘$1’)} |
```
  
I’d recommend for you to make a little step back and take your time to learn the basics of Powershell. You cannot learn a technology properly by guessing.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/get-the-next-available-unused-ad-computername-and-ou-location/11579/8 "2024-05-16T20:33:47Z")

</div>


