I used the following PowerShell script to make new AD users, and after the user is created, a user id and password are displayed in Powershell. Now, I will like to have this userID and password to be sent automaticall to the new user with a pdf attachment on how to reset the password using an SMTP server.
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from user.csv in the $user variable
$ADUsers = Import-csv d:useraccount\user.csv
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$FirstName = $User.FirstName
$Initials = $User.Initials
$Surname = $User.LastName
$DisplayName = $User.DisplayName
$Path = $User.Path
$SamAccountName = $User.SamAccountName
$Password = $User.Password
$Description = $User.Description
if (Get-ADUser -F {SamAccountName -eq $SamAccountName})
{
#If user does exist, give a warning
Write-Warning "A user account with Logon Name $SamAccountName already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $SamAccountName `
-UserPrincipalName "$SamAccountName@DCNAME" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Initials $Initials `
-DisplayName $DisplayName `
-Description $Description `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
add-adgroupmember $MemberGroup $SamAccountName
}
}
My Question is, has anyone does this of have a sample script that i can reference to complete this task. I have installed and configured SMTP, but not really sure how to add a script to trigger the email upon new user creation.