First of all, create an Excel spreadsheet with .csv file format as shown in the attached screenshot…

Now create and run the PowerShell script as shown below…

# Import the Active Directory Powershell Module
Import-Module ActiveDirectory


# Create OU
New-ADOrganizationalUnit -name Development -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False
New-ADOrganizationalUnit -name IT -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False
New-ADOrganizationalUnit -name Managers -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False
New-ADOrganizationalUnit -name Marketing -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False
New-ADOrganizationalUnit -name Research -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False
New-ADOrganizationalUnit -name Sales -path "OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" -ProtectedFromAccidentalDeletion $False


# Creating Group
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name Development -GroupScope Global -GroupCategory Security
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name IT -GroupScope Global -GroupCategory Security
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name Managers -GroupScope Global -GroupCategory Security
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name Marketing -GroupScope Global -GroupCategory Security
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name Research -GroupScope Global -GroupCategory Security
New-AdGroup -path "OU=SecurityGroups,OU=Accounts,DC=sccmlogs,dc=com" -Name Sales -GroupScope Global -GroupCategory Security


# Store the data from NewUsersFinal.csv in the $ADUsers variable
$ADUsers = Import-Csv "C:\temp\NewUsers.csv"

# Define UPN
$UPN = (Get-ADDomain).dnsroot

# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
    try {
        # Define the parameters using a hashtable
        $UserParams = @{
            SamAccountName        = $User.username
            UserPrincipalName     = "$($User.username)@$UPN"
            Name                  = "$($User.firstname) $($User.lastname)"
            GivenName             = $User.firstname
            Surname               = $User.lastname
            Enabled               = $True
            DisplayName           = "$($User.firstname) $($User.lastname)"
            Path                  = $User.ou #This field refers to the OU the user account is to be created in
            City                  = $User.city
            PostalCode            = $User.zipcode
            Company               = $User.company
            Country               = $User.country
            State                 = $User.state
            StreetAddress         = $User.streetaddress
            OfficePhone           = $User.telephone
            EmailAddress          = $User.email
            Title                 = $User.jobtitle
            Department            = $User.department
            AccountPassword       = (ConvertTo-secureString $User.password -AsPlainText -Force)
            ChangePasswordAtLogon = $False
        }

        # Check to see if the user already exists in AD
        if (Get-ADUser -Filter "SamAccountName -eq '$($User.username)'") {

            # Give a warning if user exists
            Write-Host "A user with username $($User.username) already exists in Active Directory." -ForegroundColor Yellow
        }
        else {
            # User does not exist then proceed to create the new user account
            # Account will be created in the OU provided by the $User.ou variable read from the CSV file
            New-ADUser @UserParams

            # If user is created, show message.
            Write-Host "The user $($User.username) is created." -ForegroundColor Green
        }
    }
    catch {
        # Handle any errors that occur during account creation
        Write-Host "Failed to create user $($User.username) - $_" -ForegroundColor Red
    }
}


#Adding Users Member of Group
Get-Aduser -Filter * -SearchBase "OU=Development,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof Development
Get-Aduser -Filter * -SearchBase "OU=IT,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof IT
Get-Aduser -Filter * -SearchBase "OU=Managers,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof Managers
Get-Aduser -Filter * -SearchBase "OU=Marketing,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof Marketing
Get-Aduser -Filter * -SearchBase "OU=Research,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof Research
Get-Aduser -Filter * -SearchBase "OU=Sales,OU=UserAccounts,OU=Accounts,DC=sccmlogs,DC=com" | Add-ADPrincipalGroupMembership -Memberof Sales

Now, check the Active Directory Users and Computers to verify the OUs and Groups and Users…

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top