CoreyCBurton
CoreyCBurton

Reputation: 33

Import-Module ActiveDirectory powershell error

I am creating a script that asks the user for the first and last name and implements it in a group in Active Directory. Below shows how the script starts

Import-Module ActiveDirectory

#Get-Command New-ADUser -Syntax

$firstName = Read-Host -Prompt "Please enter the first name"
$lastName = Read-Host -Prompt "Please enter the last name"

The text below shows the body of the script where the information is put

New-ADUser ` 
    -Name "$firstName $lastName" `
    -GivenName $firstName `
    -Surname $lastName `
    -UserPrincipalName = "$firstName.lastname"
    -EmailAddress "$firstName.$lastName@<domain>"
    -ChangePasswordAtLogon 1 `
    -Enabled 1 `
    -StreetAddress "<info>" `
    -Office "<info>" `
    -State "<info>" `
    -PostalCode "<info>" `
    -Country "<info>" `
    -Path "<path>" 

I get an error that is shown below showing that objects are not found. The errors are shown below

-Name$firstName $lastName : The term '-Name$firstName $lastName' 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 line:9 char:5
+     -Name"$firstName $lastName" `
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Name$firstName $lastName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-EmailAddress : The term '-EmailAddress' 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 line:13 char:5
+     -EmailAddress "[email protected]"
+     ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-EmailAddress: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 line:14 char:5
+     -ChangePasswordAtLogon 1 `
+     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ChangePasswordAtLogon:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I have checked spelling and that doesnt seem to be an issue. Is there an obvious mistake I am making?

Upvotes: 0

Views: 221

Answers (3)

TheMadTechnician
TheMadTechnician

Reputation: 36342

This looks like you have a space after the backtick character. That is the danger of using that to do line breaks. A better way to do that if you like how it is organized better is to define a hashtable, then splat that to the cmdlet like this:

$UserParams = @{
    Name = "$firstName $lastName"
    GivenName = $firstName
    Surname = $lastName 
    UserPrincipalName = "$firstName.lastname"
    EmailAddress = "$firstName.$lastName@<domain>"
    ChangePasswordAtLogon = 1 
    Enabled = 1 
    StreetAddress = "<info>" 
    Office = "<info>" 
    State = "<info>" 
    PostalCode = "<info>" 
    Country = "<info>" 
    Path = "<path>" 
}
New-ADUser @UserParams

Upvotes: 1

Robbert
Robbert

Reputation: 6592

Your example shows a space between -name and "$firstName $lastName" but the error shows differently. Double check that there's a space

You're also missing some backticks, one after the UserPrincipalName line and one after emailaddress line and you have an extra space after the first backtick.

New-ADUser `
  -Name "$firstName $lastName" `
  -GivenName $firstName `
  -Surname $lastName `
  -UserPrincipalName = "$firstName.lastname" `
  -EmailAddress "$firstName.$lastName@<domain>" `
  -ChangePasswordAtLogon 1 `
  -Enabled 1 `
  -StreetAddress "<info>" `
  -Office "<info>" `
  -State "<info>" `
  -PostalCode "<info>" `
  -Country "<info>" `
  -Path "<path>" 

Upvotes: 0

Karolina Ochlik
Karolina Ochlik

Reputation: 938

Remove trailing whitespaces after backticks (`) and place missing ones :)

New-ADUser `
    -Name "$firstName $lastName" `
    -GivenName $firstName `
    -Surname $lastName `
    -UserPrincipalName = "$firstName.lastname" `
    -EmailAddress "$firstName.$lastName@<domain>" `
    -ChangePasswordAtLogon 1 `
    -Enabled 1 `
    -StreetAddress "<info>" `
    -Office "<info>" `
    -State "<info>" `
    -PostalCode "<info>" `
    -Country "<info>" `
    -Path "<path>" 

Upvotes: 0

Related Questions