I have a big problem with AD windows server.
Now I am trying to create AD account by using powershell script and python script.
When I create AD account with powershell script, it include behind of CN name. For example, I created TEAM3 account in AD with powershell script. In the distinguishedName in Attribute editor in AD, the CN name is show like this CN=TEAM3.OU=User….and so on.

I use the csv file to store the AD account Username,Password,FirstName,DisplayName,UPN,LastName and Powershell script will execute the account creation from csv file.

Let me provide my code,
by the way, I am not powershell expert, I just trying and those code I reference from CHATGPT.
I Really Appreciate it.
Thanks

`Import-Module ActiveDirectory
$userDetails = Import-Csv -Path $csvPath

# Establish a connection to the domain controller
#$credential = Get-Credential

#Import-PSSession -Session $session -Module ActiveDirectory

foreach ($user in $userDetails) {
   # Set the user properties
   $username = $user.Username
   $firstname = $user.FirstName
   $lastname = $user.LastName
   $displayname = $user.DisplayName
   $upn = $user.UPN
   $mypassword = $user.Password

    # Prompt for password
    #$password = Read-Host "Enter password for user '$username'" -AsSecureString
    $password = ConvertTo-SecureString -String $mypassword -AsPlainText -Force

    # Create the user account
    $newUser = New-ADUser -SamAccountName $username -UserPrincipalName $upn -GivenName $firstname -        Surname $lastname -DisplayName $displayname -Name "$firstname $lastname" -AccountPassword $password -  Enabled $true -Path $ou

    $newUser = Get-ADUser -Identity $username

    Write-Host "New User: $($newUser.SamAccountName)"
    # Set additional user properties if needed
    # Set-ADUser -Identity $newUser -Description "Some description"
    # Set-ADUser -Identity $newUser -Office "Some office"
    # Add the user to a specific group
    $group = "SVP Users"  # Replace with the name of the group you want to add the user to
    Add-ADGroupMember -Identity $group -Members $newUser
    Write-Host "User account created: $username"

}

Remove the session after the loop is completed

#Remove-PSSession $session`

I want to remove that from CN in AD.

New contributor

Sai Thiha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.