1

I am very very new to PowerShell. I am trying to create a script to create new user in AD, with prompts for each switch, but I am struggling a little with the Syntax and was hoping someone could help me.

Currently I have:

$FirstName = Read-Host -Prompt "Please enter the first name"

$LastName = Read-Host -Prompt "Please enter the last name"

New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname $LastName

The above gives me the prompts that I want, but then when I input the last name, it returns the following error:

"New-ADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or 
empty, and then try the command again.
At line:3 char:18
+ New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname  ...
+                  ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser
"

I assume this is because I have not put the correct entry in after -Name.

Hopefully one of you can help me and give me an explanation & example as to what I should do.

2
  • Possible duplicate of Unable to get PowerShell code to work Commented Mar 22, 2018 at 21:27
  • 1
    try formatting $FirstName and $LastName to strings. New-ADUser -Name [string]::Format( "{0}.{1}", $FirstName, $LastName) -GivenName $FirstName -Surname $LastName Commented Mar 22, 2018 at 21:31

1 Answer 1

3

Change this line:

New-ADUser -Name $FirstName.$LastName -GivenName $FirstName -Surname $LastName

to:

New-ADUser -Name "$FirstName.$LastName" -GivenName $FirstName -Surname $LastName
Sign up to request clarification or add additional context in comments.

1 Comment

To clarify what's going on, the issue is that the period is an operator. $FirstName.$LastName means "For the object represented by the variable $FirstName, get the value of the property whose name is the value of the variable $LastName." In other words, 'John'.'Smith' and not 'John.Smith'. For a working example: $Drive = Get-Item C:\; $Property = 'PSIsContainer'; $Drive.$Property should output True because the property PSIsContainer of $Drive has a value of true. If the property doesn't exist, however, you'll just get a null value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.