locked
ad script shell to add users RRS feed

  • Question

  • please i have a problem with my powershell script i want to make a script that adds a lot of users with specific attributes  and also set the default password as the name of each user but i(m getting errors .

    here's the script 

    Import-Csv "C:\usersliste.csv" | ForEach-Object {$userprinicpalname = $_.SamAccountName + "@stagaire.local" $password = $_.GivenName | ConvertTo-SecureString -AsPlainText -Force New-ADUser -Name $_.name -GivenName $_.GivenName -Surname $_.Surname -SamAccountName $_.SamAccountName -UserPrincipalName $userprinicpalname -AccountExpirationDate $_.AccountExpirationDate -Title $_.Title -Department $_.Department -Company $_.Company -ScriptPath $_.ScriptPath -Path $_.Path -AccountPassword $password -Enabled $True -PasswordNeverExpires $true -PassThru }

    heeeeelp please 

    Thursday, June 15, 2017 7:21 PM

All replies

  • Please look in Gallery for scripts that do this.

    The password must be encrypted before it can be set.  The Gallery scripts will show how this is done.


    \_(ツ)_/

    Thursday, June 15, 2017 7:52 PM
  • Can you paste the error?

    Thursday, June 15, 2017 7:58 PM
  • Hi Leo, 

    do it like that: 

     $userlist = import-csv .\namensliste.csv -Delimiter ';'
    $targetou = "OU=Users,OU=US,OU=Source,DC=mysourcedomain,DC=local"
    
    $userlist | foreach-object {
    	$id = $_.id
    	$firstname = $_.vorname
    	$lastname = $_.nachname
    	$fullname = $_.nachname_vorname
    	$logonname = $_.logonname
    	$upn = $_.logonname + "@company.local"
    	$password =  convertto-securestring -force- asPlainText $_.password
    
    	New-ADUser -Name "$fullname" -SamAccountName $logonname -UserPrincipalName $upn -AccountPassword $password -Path "$targetOU" -EmailAddress $upn -Enabled $true
    }
    

    Let us know if it helped.

    Regards, Martin

    • Proposed as answer by Hello_2018 Friday, June 16, 2017 6:28 AM
    Thursday, June 15, 2017 8:14 PM
  • thanks i'wll try this right now !
    Thursday, June 15, 2017 8:56 PM
  • i donr understand how to make this encripted

    Thursday, June 15, 2017 9:06 PM
  • i still having prolems i modified martin's script and also having problems on execution :

    $userlist = import-csv c:\userssliste.csv -Delimiter ';'
    $userlist | foreach-object { $name= $_.name $givenname = $_.givenname $surname = $_.surname $SamAccountName = $_.SamAccountName $AccountExpirationDate = $_.AccountExpirationDate $upn = $_.name + "@stagare.local" $password =  convertto-securestring -force- asPlainText $_.name New-ADUser -Name $name  -givenname $givenname -surname $surname -SamAccountName $SamAccountName -AccountExpirationDate $AccountExpirationDate -Title $_.Title -Department $_.Department -Company $_.Company $ScriptPath $_.ScriptPath -UserPrincipalName $upn -AccountPassword $password -Path $_.path -EmailAddress $upn -Enabled $true
    }

    Thursday, June 15, 2017 10:17 PM
  • It is the requirement of all technical forums that you are accurate and post all errors you are getting.  We cannot guess at what is happening when you make vague statements like "I am having problems".  Does this mean you are a problem child?  Does it mean that you computer is talking back to you?  The statement is not technically useful.

    YOU both need to attempt to format your code and post it correctly.

    LIke this

    import-csv c:\userssliste.csv | 
    	foreach-object {
    		$name = $_.name 
    		$givenname = $_.givenname 
    		$surname = $_.surname 
    		$SamAccountName = $_.SamAccountName 
    		$AccountExpirationDate = $_.AccountExpirationDate 
    		$upn = $_.name + "@stagare.local" 
    		$password = convertto-securestring -force- asPlainText $_.name 
    		New-ADUser -Name $name -givenname $givenname -surname $surname -SamAccountName $SamAccountName -AccountExpirationDate $AccountExpirationDate -Title $_.Title -Department $_.Department -Company $_.Company $ScriptPath $_.ScriptPath -UserPrincipalName $upn -AccountPassword $password -Path $_.path -EmailAddress $upn -Enabled $true
    }

    Until you learn basic PowerShell all of this will be very hard for you to understand or work with.  Start with absics before trying to accomplish harder tasks.


    \_(ツ)_/

    Thursday, June 15, 2017 10:23 PM
  • yes i know that i have to explain exactly what the problem but i have the system in frensh so i(m uste trying to diescribe it quickly ,and i'm very thankfull of your help
    Thursday, June 15, 2017 10:28 PM
  • "Splatting" can make all of this much easier:

    import-csv c:\userssliste.csv | 
    	foreach-object {
    		$splat = @{
    			Name = $_.name 
    			givenname = $_.givenname 
    			surname = $_.surname 
    			SamAccountName = $_.SamAccountName 
    			AccountExpirationDate = $_.AccountExpirationDate
    			UserPrincipalName = $_.name + '@stagare.local'
    			AccountPassword = convertto-securestring -force- asPlainText
    			Path = $_.path
    			ScriptPath = $_.ScriptPath
    			Department = $_.Department
    			EmailAddress = $_.name + '@stagare.local'
    			Enabled = $true
        	        }
    		New-ADUser @splat
    	}
    


    \_(ツ)_/

    Thursday, June 15, 2017 10:29 PM
  • yes i know that i have to explain exactly what the problem but i have the system in frensh so i(m uste trying to diescribe it quickly ,and i'm very thankfull of your help

    No excuse.  In French or English the error is still required.  We cannot guess at what you are asking with no information. 


    \_(ツ)_/


    • Edited by jrv Thursday, June 15, 2017 10:31 PM
    Thursday, June 15, 2017 10:31 PM
  • this is what i(m having as  error

    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a
    Argument that is not null or empty and try again.
    At character C: \ ss.ps1: 17: 14
    + New-ADUser @splat
    + ~~~~~~
        + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser

    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    New-ADUser: Unable to validate argument on parameter "Name". The argument is null or empty. Indicate a Argument that is not null or empty and try again. At character C: \ ss.ps1: 17: 14 + New-ADUser @splat + ~~~~~~     + CategoryInfo: InvalidData: (:) [New-ADUser], ParameterBindingValidationException     + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft.ActiveDirectory.Management.Commands.NewADUser
    Thursday, June 15, 2017 10:35 PM
  • i modified the script by a space before 

    -force- asPlainText

    and now i get the request to enter the string value but normally it has to be automaticly given

    Thursday, June 15, 2017 10:41 PM
  • Please learn how to correctly post code and errors.  I showed you that above.  Please take the time to learn basic PowerShell.  We cannot write this script for you one error at a time.

    If you have null entries then you may have a bad CSV file.  You cannot have empty settings.

    Load your Csv and inspect the records for correctness.

    Help Import-Csv -Full


    \_(ツ)_/

    Thursday, June 15, 2017 11:01 PM