PowerShell New-ADUser Multiple Variables for AccountPassword

Jason Russell 20 Reputation points
2023-02-23T17:37:28.43+00:00

I have 50+ Active Directory users to create and I am attempting to do so with PowerShell. The idea is to have the username contain a number and increment by one through the loop. I'm also attempting to do the same increment with the password. The following script works with the exception of the password. I cannot figure out how to get the password to work.

The ideal result will be the users will be created like this...

Username: Training1 Password: Training1#5076

Username: Training2 Password: Training2#5076

etc, etc, etc,.

I have tried entering a simple string for the $PwCount and it works. I have tried using a variable for $PwCount and it works. And I have tried just entering a string for AccountPassword and it works. Where things break, is when I try to use multiple varialbes, in this case, including the $counter. Each time I attempt to run the script, I receive error: Cannot bind parameter 'AccountPassword'. Cannot convert the "System.Security.SecureString" value of type "System.String" to type "System.Security.SecureString".



Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-02-23T19:50:07.52+00:00

    I think you're looking for something like this:

    1..50 |
        ForEach-Object{
            $acct = "Training{0}" -f $_
            $secpw = ConvertTo-SecureString ("Training{0}#5076" -f $_) -AsPlainText -Force
            New-ADUser -Name $acct -AccountPassword $secpw
    
        }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-02-23T21:12:25.6366667+00:00

    Does this work for you?

    $FirstName = "Training"
    $Pwd1 = "Training"
    $Pwd2 = "#5076"
    $repeat = 1 #Change this to 50
    $Groups = "Training","Remote"
    $UserPath = "OU=Training,OU=ABC_Users,DC=ABC,DC=local"
    $GroupPath = "OU=ABC_Groups,DC=ABC,DC=local"
    
    1..$repeat |
        ForEach-Object{
            $FnCount = "{0}{1}" -f $FirstName, $_
            $PwdCount = ConvertTo-SecureString ("{0}{1}{2}" -f $Pwd1, $_, $Pwd2) -AsPlainText -Force
    
            $UserObject = @{
                AccountPassword = $PwdCount
                Name = $FnCount
                GivenName = $FnCount
                PasswordNeverExpires = $True
                Path = $UserPath
                SamAccountname = $FnCount
                UserPrincipalName = "{0}@ABC.local" -f $FnCount
                Enabled = $True
                DisplayName = $FnCount
                CannotChangePassword = $True
            }
            $g = ""    # Used in catch block
            try{
                $u = New-ADUser @UserObject -ErrorAction STOP
                #Add account to groups
                ForEach ($group in $Groups){
                    $g = Get-ADGroup -Server "abc.local" -SearchBase $GroupPath -Filter "name -like '$group'"
                    if ($null -ne $g){
                        $g | Add-ADGroupMember -Members $u.distinguishedName -ErrorAction STOP
                    }
                    else{
                        Write-Host "could not find '$group'" -ForegroundColor Red
                    }
                }
            }
            catch{
                Write-Host "Failed to create user '$fncount', or failed to add $fncount to group $($g.distinguishedName)" -ForegroundColor Red
                Write-Host $_
            }   
        } 
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.