Reece Alqotaibi
Reece Alqotaibi

Reputation: 11

How Do I Use a string a variable in Powershell?

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.

Upvotes: 1

Views: 542

Answers (1)

boxdog
boxdog

Reputation: 8432

Change this line:

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

to:

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

Upvotes: 3

Related Questions