1

I am working on a script creating a new user via PowerShell with user (creator) input. The input I am looking for is for the first name and last name along with some attributes. I would like the samaccountname and the UPN to be auto created from the input. Not sure if this can be done completely but would like to get some input on my current script. I highlighted firstinital as a placeholder to show what I am trying to accomplish.

new-aduser -givenname($givenname = read-host "Input Firstname") -surname($surname = read-host "Input Lastname") -samAccountName ("***firstinitial***"+"$._surname") -userprincipalname "$._surname+"@domain.com" -path "OUName" -whatif

Alrighty thanks for the help below. I was able to do a few more searches and can up with the following. All looks to work except the distingushed name comes up as a single name instead of a space between the first and last name.

#User info entered
$first = Read-Host "First name"
$last = Read-Host "Last name"
$title = Read-Host "Title"
$location = Read-Host "Location"
$department = Read-Host "Business Practice"
$password = read-host -assecurestring "Password"

#Create new user

$Attributes = @{

   Enabled = $true
   ChangePasswordAtLogon = $false

   UserPrincipalName = $first.split(" ")[0]+$last+"@domain.com"
   Name = $first+$last
   GivenName = $first
   Surname = $last
   DisplayName = "$first "+" $last"
   Office = $location
   Department = $department
   Title = $title
   samAccountName = $first.split(" ")[0] + $last
   AccountPassword = $password

}

New-ADUser @Attributes -whatif
3
  • And to clarify, if I use the name of Jane Doe, I get the following - (notice no space in JaneDoe) "CN=janedoe,CN=Users,DC=KFriese,DC=office" Commented Oct 26, 2020 at 18:22
  • Ok, figured this one out the name combining issue. Commented Oct 26, 2020 at 18:40
  • $first + " " + $last or "{0} {1}" -f $first, $last Commented Oct 26, 2020 at 20:57

2 Answers 2

1

You can add this to get the $_.givenName as the first initial:

$gn = (read-host "Input Firstname")
$sn = (read-host "Input Lastname")
new-aduser -givenname $gn -surname $sn -samAccountName $gn.split(" ")[0]+$sn -userprincipalname $sn+"@kfriese.com" -path "OUName" -whatif
Sign up to request clarification or add additional context in comments.

1 Comment

$gn.split(" ")[0]+$sn -- this didnt work. it combines the first and last name. I need the first initial and the last name as the SAM.
0

Here is a more advanced and robust way to do it: a custom function, that makes use of PowerShell integrated functionality.

It uses attributes that make the parameters mandatory, so user input will automatically be inquired when the function is called. Also a validation attribute to make sure the input is not empty and has no invalid characters (you might want to adjust the regex according to your needs).

The arguments for New-ADUser are passed using splatting. The rest is pretty straight-forward...

function makeuser {
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidatePattern("[a-z]+")]
        [string]$GivenName,

        [Parameter(Mandatory, Position = 1)]
        [ValidatePattern("[a-z]+")]
        [string]$Surname
    )
    $params = @{
        GivenName = $GivenName
        Surname = $Surname
        SamAccountName = $GivenName[0] + $Surname
        UserPrincipalName = $Surname + "@kfriese.com"
        Path = "OUName"
    }
    New-AdUser @params
}

To call the function, just type (parameter values will be inquired automatically)

makeuser

Or specify the values explicitly:

makeuser -GivenName Foo -Surname Bar
# or
makeuser foo bar

Comments

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.