# Simple do..while - check for user account existence.

**URL:** <https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662>\
**Category:** PowerShell Help\
**Created:** [July 14, 2020, 1:00pm UTC](https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662 "2020-07-14T13:00:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcosis](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@marcosis](https://forums.powershell.org/u/marcosis)\
**Post date:** [July 14, 2020, 1:00pm UTC](https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662/1 "2020-07-14T13:00:55Z")

</div>

Hello Powershell.org Community,

First time poster, semi long term reader. Just need help with what I think will be straight forward for the professionals out there.

I'm trying to generate a username based on the first initials of their first and last name, and then adding 'user' on the end, ie 'SMUser'. However sometimes that username is taken, so I would like to take the second character of the last name, and add that to the user name ie 'SMcUser', this is easy enough to do, but I'm trying to take it further and ask it to check AD for an existing account, if it finds it, it extends the username even further by adding another character. I have never used do..while before, and I can see the problem, however I'm not sure if I'm going about it the right way.

Any help appreciated, oh and the silentlycontinue looks to be the main issue... it doesn't continue.

`$FirstName = 'Steve'`

`$LastName = 'McQueen'`

`do {`

` $x = 0`

` $NewAlias = $FirstName[0], $LastName.Substring(0, $x), 'User' -join ""`

` $UserAlias = $NewAlias.ToLower()`

` write-output $UserAlias`

` $x = $x++`

`} while ([bool](Get-ADUser -Identity $UserAlias -ErrorAction SilentlyContinue) -eq $true)`

---

<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:** [July 14, 2020, 2:08pm UTC](https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662/2 "2020-07-14T14:08:46Z")

</div>

Is it just a typo here in your post? … the variable you use in your Get-ADUser query is different to the variable inside your while loop. $UserAlias vs. $UserAlia

---

<div class="post-metadata">

**Author:** ![marcosis](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@marcosis](https://forums.powershell.org/u/marcosis)\
**Post date:** [July 14, 2020, 2:45pm UTC](https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662/3 "2020-07-14T14:45:42Z")

</div>

Thanks, edited 🙂 That was me fixing up variables for privacy reasons.

---

<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:29pm UTC](https://forums.powershell.org/t/simple-do-while-check-for-user-account-existence/14662/4 "2024-05-16T20:29:12Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


