New-aduser -accountpassword " cannot convert

:frowning: ello,

I try to understand my error in creating new adusers from CSV.

Powershell tells me that I did something wrong it seems…:frowning:
Hopefully you can tell me where I ,
As far as I can see, the point is the creation of the accountpasword.
I tried a number of things, wothout succes .

My (partial) script to create a new-aduser:

typ
    $Username = foreach($account in $CSV) {
# Check if the ADUSer alrady exists
    if (!(Get-ADUser -Filter "samaccountName -eq '$($account.Sanaam)'" -Server $account.Domein))
    {
      Write-Host "The '$($account.sanaam)' is not yet in '$($account.Domein)' and will now be created" -ForegroundColor Yellow
    
    }
    else
    {
     Write-Host "!!! The '$($account.sanaam)' is already in $($account.Domein) and can not be created !!!" -ForegroundColor Red 
    }

# Create Account password
    $SAPassword = (New-ReadablePassword -PasswordLength 21)
    $accountpw = "$SAPassword"| ConvertTo-SecureString  -AsPlainText -Force
 
# Create new user with parameters
    New-ADUser  -Samaccountname = $account.sanaam `
                -Userprincipalname =  $account.sanaam  + "@" +$($account.domein) `
                -Name = $account.sanaam `
                -Givenname = $account.sanaam `
                -Displayname = $account.sanaam `
                -Server = $Domain `
                -AccountPassword = $SAPassword | ConvertTo-SecureString -AsPlainText -Force `
                -Path = $($account.path) `
                -Description = $account.motivatie + " " + "|" + $account.wnr `
                -PasswordNeverExpires = $true `
                -ChangePasswordAtLogon = $false `
                -CannotChangePassword = $false `
                -Enabled =$true `
                -Passthru = $true `
                -Enabled = $true `
                -Whatif = $WhatIfPreference
             }

The error it shows me:

New-ADUser : Cannot bind parameter 'AccountPassword'. Cannot convert the "=" value of type "Sys
tem.String" to type "System.Security.SecureString".
At line:25 char:34
+                 -AccountPassword = $SAPassword | ConvertTo-SecureStri ...
+                                  ~
    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Managem 
   ent.Commands.NewADUser

I tried to create a variabele of the securstring and use this variabele in " Accountpassword = "
This is not working however.

Looking forward to learn en understand .

Parameters do not use ‘=’. Remove all of those from the New-ADUser function call.

2 Likes

Instead of using backticks as line continuation characters what’s actually quite error prone you should take a look at

2 Likes

if you like to use ‘=’ for parameter you can try:

$Parameters = @{
Samaccountname = $account.sanaam
Userprincipalname = $account.sanaam + “@” +$($account.domein)
Name = $account.sanaam

}
New-ADUser @Parameters

*** No “-” before a property and no “`” at the end as your current