I am looking to automate creation of new AD Users from a csv. I need to assign a UPN suffix, I’m struggling to decide if to make that into a separate function or module, it is a simple task but after reading Powershell scriping and toolmaking it makes a point to make tools that are testable and only do one thing. Is this concept applicable here? example below, I haven’t finished the switch statement, thats just a placeholder. I find that with every task I need to do I keep thinking about the concept of tools even though these sub tasks aren’t really going to be used anywhere else and they are pretty basic things.
If you have any other comment on the script please feel free to do so.
[CmdletBinding()]
param (
#Path to folder that contains new user csv files
[Parameter()]
[string]
$path
)
$UserList = Import-Csv -Path $path
foreach ($User in $UserList) {
#Set the UPN Suffix to be used in the UserPrincipalName
switch ($user.department) {
condition { $UPNSuffix = }
} #switch
$Attributes = @{
Name = "$($User.firstname) $($User.surname)"
DisplayName = "$($User.firstname) $($User.surname)"
UserPrincipalName = "$($User.firstname).$($User.surname)$UPNSuffix"
SamAccountName = "$($User.firstname).$($User.surname)"
GivenName = $User.firstname
Surname = $User.surname
Company = $User.Company
Department = $User.Department
Title = $User.Title
Description = $user.Description
Manager = $user.Manager
Office = $user.Office
}
New-ADUser @Attributes
} #foreach $user in $UserList