I have put together a script to create new user accounts in AD from a CSV File but I'm having a few issues with the copy user part. So it works when every new user has a copy user but when they don't provide a copy user and they leave this blank it's failing to create the variable and using the copy user variable from the previous row. Any ideas how I could amend this to only run the copy user part if the copy user cell is filled in on the csv file?

$ADUsers = Import-Csv "C:\Bulk Create New Users\New Users.csv"
ForEach ($User in $ADUsers)
    $Server = "DomainController"
    $FirstName = $User.FirstName
    $Surname = $User.Surname
    $ExtraCharacter = $User.ExtraCharacter
    $EmailSuffix = $User.EmailSuffix
    $BusinessUnit = $User.BusinessUnit
    $JobTitle = $User.JobTitle
    $Description = $User.Description
    $Company = $User.Company
    $Office = $User.Office
    $OU = $User.OU
    $Password = $User.Password
    $CopyUser = Get-ADUser -Identity $User.CopyUser -Properties MemberOf
    $IntranetGroup = $User.IntranetGroup
    $DisplayName = $User.DisplayName
    $Username = $User.Username
    $Email = $User.Email
    New-ADUser -Server $Server -Name $DisplayName -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -Force) -Company $Company -Description $Description -DisplayName $DisplayName -EmailAddress $Email -Enabled $True -GivenName $FirstName -Office $Office -OtherAttributes @{'extensionAttribute10'=$Email;'mailNickname'=$Username} -Path $OU -SamAccountName $Username -Surname $Surname -Title $JobTitle -UserPrincipalName $Email
    Set-ADUser -Server $Server -Identity $Username -Add @{proxyAddresses ="SMTP:" + $Email}
    Add-ADPrincipalGroupMembership -Server $Server -Identity $Username -MemberOf "Standard","Member","Groups"

#Copy membership groups from the copy user to the new user & then remove the Equitrac group $CopyToNewUser = Get-ADUser -Server $Server -Identity $UserName -Properties MemberOf $CopyUser.MemberOf | Where-Object {$CopyToNewUser.MemberOf -notcontains $_} | Add-ADGroupMember -Server $Server -Members $CopyToNewUser Remove-ADPrincipalGroupMembership -Server $Server -Identity $UserName -MemberOf "Equitrac Print Users" -Confirm:$False}
| improve this question | | | | |
  • 1
    Instead of copying the csv data into intermediate variables, you should read about splatting to simplify/streamline your foreach. also check 1st if there IS a copyuser and if not continue to the next. – LotPings Aug 5 '19 at 21:13
  • Thanks for the reply and the link on splatting, will have a read up on it. How would you get Powershell to check if there is a copy user? That's the part I was struggling with, couldn't find anything helpful when searching. – Jim Thomas Aug 6 '19 at 10:01
  1. check if the property .CopyUser is empty
  2. check if Get-ADUser did return something for $User.CopyUser
$ADUsers = Import-Csv "C:\Bulk Create New Users\New Users.csv"
ForEach ($User in ($ADUsers|Where {$_.CopyUser -ne ''}) ) {
   if(!($CopyUser = Get-ADUser -Identity $User.CopyUser -Properties MemberOf)){continue}
| improve this answer | | | | |
  • Wouldn't that first point completely skip creating that user if the copy user was empty? I'd only want it to skip copying the membership groups. – Jim Thomas Aug 6 '19 at 11:10
  • Well, I did (mis-)understood you that way. But you can insert an if inside the foreach at any point to conditionally execute code parts. – LotPings Aug 6 '19 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.