Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $unameFirst = "Jon"
- $unameLast = "Jones"
- $unameMiddle = ""
- $unamefirst = $unameFirst.ToLower()
- if ($unameMiddle) {
- $unameMiddle = $unameMiddle.ToLower()
- $unameMiddle = $unameMiddle.split(".")[0]
- }
- $unameLast = $unameLast.ToLower()
- if ($unameMiddle) {
- $possibleNames = 1..$unameFirst.Length | ForEach-Object {
- -join ($unameFirst.Substring(0,$_),$unameMiddle,$unameLast)
- }
- }
- else {
- $possibleNames = 1..$unameFirst.Length | ForEach-Object {
- -join ($unameFirst.Substring(0,$_),$unameLast)
- }
- }
- #iterate through the possible names until we find one that's not in use
- foreach ($possibleName in $possibleNames) {
- Write-Host "Trying username $possibleName"
- try {
- $usernameToCheck = Get-ADUser $possibleName
- }
- catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
- }
- if (!$usernameToCheck) {
- write-host "Found available username: $possibleName"
- break
- }
- else { Write-Host "Username $possibleName taken"}
- }
Advertisement
Add Comment
Please, Sign In to add comment