Share via


Powershell, Import AD users with new-aduser

Question

Wednesday, April 26, 2017 10:44 AM

This is the code i use (see below), can you please tell me what i'm doing wrong

$csvcontent = Import-CSV -Path d:\temp\users1.csv -Delimiter ','

foreach ($user in $csvcontent)

{

New-ADUser
 -AccountPassword (ConvertTo-SecureString “Welcome1!” -AsPlainText -Force)
 -ChangePasswordAtLogon $false
 -Company “RWS”
-DisplayName ($user.Firstname+” “+$user.Lastname)
 -Enabled $true
 -Mail $user.Mail
 -MobilePhone ($user.Phone)
 -Name ($user.Firstname+” “+$user.Lastname)
 -SamAccountName ($user.Account)
 -Title “Testgebruiker”
  -Path “OU=Acceptation,OU=User,OU=Accounts,OU=UserManagement,DC=Domain,DC=local”
-state "South-Holland Netherlands"
 -givenname $user.Firstname
 -surname $user.Lastname
 -userprincipalname (“@Domain.local”)
-department “Team”
-description “IFM - Testgebruiker”
}

This is the CSV:

#CSV

#Account,Firstname,Lastname,Phone,Mail

#testuser1,Hans,Janzen,+316-12345678,[email protected]

#testuser2,Harry,Las,+316-45678910,[email protected]

Error hat i get:

PS C:\Users\adm-ad-borgelda91> D:\Scripts\Import-NewAdUser.ps1

cmdlet New-ADUser at command pipeline position 1

Supply values for the following parameters:

Name: 

IF i enter a name

Then i get this:

New-ADUser : Access is denied

At D:\Scripts\Import-NewAdUser.ps1:10 char:1

+ New-ADUser

+ ~~~~~~~~~~

    + CategoryInfo          : PermissionDenied: (CN=test,CN=Users,DC=TNACAD,DC=local:String) [New-ADUser], UnauthorizedAcce

   ssException

    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.UnauthorizedAccessException,Microsoft.ActiveDirectory.Management

   .Commands.NewADUser

-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:11 char:2

+  -AccountPassword (ConvertTo-SecureString “Welcome1!” -AsPlainText -Force)

+  ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-ChangePasswordAtLogon : The term '-ChangePasswordAtLogon' is not recognized as the name of a cmdlet, function, script

file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and

try again.

At D:\Scripts\Import-NewAdUser.ps1:12 char:2

+  -ChangePasswordAtLogon $false

+  ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-ChangePasswordAtLogon:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Company : The term '-Company' is not recognized as the name of a cmdlet, function, script file, or operable program. Check

the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:13 char:2

+  -Company “RWS”

+  ~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-Company:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-DisplayName : The term '-DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable

program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:14 char:1

+ -DisplayName ($user.Firstname+” “+$user.Lastname)

+ ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-DisplayName:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Enabled : The term '-Enabled' is not recognized as the name of a cmdlet, function, script file, or operable program. Check

the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:15 char:2

+  -Enabled $true

+  ~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-Enabled:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Mail : The term '-Mail' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:16 char:2

+  -Mail $user.Mail

+  ~~~~~

    + CategoryInfo          : ObjectNotFound: (-Mail:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-MobilePhone : The term '-MobilePhone' is not recognized as the name of a cmdlet, function, script file, or operable

program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:17 char:2

+  -MobilePhone ($user.Phone)

+  ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-MobilePhone:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Name : The term '-Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:18 char:2

+  -Name ($user.Firstname+” “+$user.Lastname)

+  ~~~~~

    + CategoryInfo          : ObjectNotFound: (-Name:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-SamAccountName : The term '-SamAccountName' is not recognized as the name of a cmdlet, function, script file, or operable

program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:19 char:2

+  -SamAccountName ($user.Account)

+  ~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-SamAccountName:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Title : The term '-Title' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:20 char:2

+  -Title “Testgebruiker”

+  ~~~~~~

    + CategoryInfo          : ObjectNotFound: (-Title:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:21 char:2

+  -Path “OU=Acceptation,OU=User,OU=Accounts,OU=UserManagement,DC=TNACAD,DC=local”

+  ~~~~~

    + CategoryInfo          : ObjectNotFound: (-Path:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-state : The term '-state' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:22 char:1

+ -state "South-Holland Netherlands"

+ ~~~~~~

    + CategoryInfo          : ObjectNotFound: (-state:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-givenname : The term '-givenname' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:23 char:2

+  -givenname $user.Firstname

+  ~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-givenname:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-surname : The term '-surname' is not recognized as the name of a cmdlet, function, script file, or operable program. Check

the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:24 char:2

+  -surname $user.Lastname

+  ~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-surname:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-userprincipalname : The term '-userprincipalname' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:25 char:2

+  -userprincipalname (“@TNACAD.local”)

+  ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-userprincipalname:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-department : The term '-department' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:26 char:1

+ -department “BOS”

+ ~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-department:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

-description : The term '-description' is not recognized as the name of a cmdlet, function, script file, or operable

program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\Scripts\Import-NewAdUser.ps1:27 char:1

+ -description “IFM - Testgebruiker”

+ ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-description:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

All replies (4)

Wednesday, April 26, 2017 10:53 AM ✅Answered | 1 vote

All of your parameters are on separate lines.  This is not how PowerShell works.   Either use line continuation or use a hash/splat.

A CSV cannot have "#" in front of each line.

Posting every error is pointless.  Only the first error is needed and valid.  YOu are getting one error for every line above.

\(ツ)_/


Wednesday, April 26, 2017 10:47 AM

It is a test domain and i'm a domain admin who has got all the right on the OU. For the OU i'm using distinguished name.


Wednesday, April 26, 2017 11:03 AM

The CSV was just part of my code file ;-). Your right, i now have more to go on. Let you know if it works.

Thanks for your quick reply.


Wednesday, April 26, 2017 11:09 AM

It works fine now.

1) No enters in the script

2) Start ISE as Administrator

3) change parameter mail into -EmailAddress