Using the New-ADUser cmdlet -Name parameter not working with a variable input

Hi, please help me with my script below. I have set a couple of variables $Fname and $Lname and are using them to fill out most of the New-ADUser cmdlet parameters for a new user account. However, it seems New-ADUser -Name $Fname $Lname doesn't work because it thinks $Lname is an argument instead of part of the string input, any ideas?

Below is the code and the error,
Thanks

function New-CompanyUser {

    param (
        [string]$Fname = (Read-Host 'Enter new user First Name only'),
        [string]$Lname = (Read-Host 'Enter new user Last Name only'),
        [string]$Description = 'New User Account',
        [string]$Password = (Read-Host 'Enter a valid password for new user account')
    )

    Begin {
        # Get OU Users
        $OUU = "OU=Users,OU=FolderIT,OU=Folder,DC=Domain,DC=Domain"
        $OUU

    }            
    Process {
        $check = Get-ADUser -Filter {Name -like $Name} -SearchBase $OUU
        
        $check
        
            if ($check) {
                write-host "The user name $Name already exist"; break
            }
            else {
                write-host "Creating a new user account $Name"
            }

        # Add new user to OU Users
        $newUser = New-ADUser -Name $Fname $Lname -GivenName $Fname -Surname $Lname -Description $Description -DisplayName $Fname $Lnam -Enabled $true -SamAccountName ($Fname + '.' + $Lname) -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -ChangePasswordAtLogon $false -Path $OUU -PassThru
      }
      End{}
}

              

Open in new window


New-ADUser : A positional parameter cannot be found that accepts argument 'Last'.
At line:31 char:20
+         $newUser = New-ADUser -Name $Fname $Lname -GivenName $Fname -Surname $Ln ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
ryanmavesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
This is a pretty easy fix. All you need to do is put quotes around $Fname and $Lname. I have modified your above script with the changes. You have to put quotes around the Name and DisplayName parameters. I have done this in the script below.

function New-CompanyUser {

    param (
        [string]$Fname = (Read-Host 'Enter new user First Name only'),
        [string]$Lname = (Read-Host 'Enter new user Last Name only'),
        [string]$Description = 'New User Account',
        [string]$Password = (Read-Host 'Enter a valid password for new user account')
    )

    Begin {
        # Get OU Users
        $OUU = "OU=Users,OU=FolderIT,OU=Folder,DC=Domain,DC=Domain"
        $OUU

    }            
    Process {
        $check = Get-ADUser -Filter {Name -like $Name} -SearchBase $OUU
        
        $check
        
            if ($check) {
                write-host "The user name $Name already exist"; break
            }
            else {
                write-host "Creating a new user account $Name"
            }

        # Add new user to OU Users
        $newUser = New-ADUser -Name "$Fname $Lname" -GivenName $Fname -Surname $Lname -Description $Description -DisplayName "$Fname $Lname" -Enabled $true -SamAccountName ($Fname + '.' + $Lname) -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -ChangePasswordAtLogon $false -Path $OUU -PassThru
      }
      End{}
}

Open in new window


Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ryanmavesAuthor Commented:
Thanks Will, actually let me test that out and see what happens again. Appreciate the suggestion. Be right back...
0
ryanmavesAuthor Commented:
Viola, you have saved the day. To think I spent over an hour trying to figure this out, I actually did the "" over the variables at one point but concluded it didn't work. What I had forgot was to put "" over the Displayname parameter...so it looked like the same error and I incorrectly assumed it didn't work. ha ha

Thank you for the lesson!
0
Will SzymkowskiSenior Solution ArchitectCommented:
Not a problem.

Glad to help!

Will.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.