Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Script to change DN attribute in AD to be the same as sAMAccountName

$
0
0

Recently I was asked to change the Display Name for all the users in AD in my organization.  I was asked to do so because the previous AD/Exchange Admin created all accounts using the default of "FirstName MiddleInitial LastName".  The new staff wanted the Display Name to be "LastName, FirstName MiddleInitial".

I made this change using this script I found on the Internet:

# Pre-Users.ps1 - Change the name & display name of existing users.
# Created by - Amit Tank
$Users = Get-User -ResultSize unlimited | where {$_.RecipientTypeDetails -eq"UserMailbox"}
ForEach ($User in $Users)
{
$DName = $User.LastName + ", "+ $User.FirstName + " "+ $user.Initials
$DName = $Dname.Trim()
Set-User $User -Name $DName -DisplayName $DName
Get-User $User | FT Name, DisplayName
$DName = $Null

}

When I made this change however it changed the DN of each user and the organization is using google sync and can no longer synchronize with google.

I think if I had a script that would change the DN to the value of the user's sAMAccount name it would be fixed. I have setup a test OU where I can test this with just one user.

I have found a few scripts that claim that they work and that it can be done, but all of them I try I get various errors. Here is one that I made based from other information I have found:

# Get sAMAccountName and then change DN to be the same. Get-ADUser -Filter * -SearchBase "OU=TEST,DC=MCCSC,DC=EDU" ForEach ($User in $Users) { set-aduser -Identity "*" $_.distinguishedname -sAMAccountName ($_.samaccountname)
}

When I try running this script I get the following error:

Set-ADUser : A positional parameter cannot be found that accepts argument '$null'.

I am a realitive PowerShell n00b so I really don't know what the heck it is saying there. I don't think I have any parameters not definded or set null, so I am not sure.

If I can get a script that will reliably change just the DN to be the same as the sAMAccountName then I can test it and I think everything will be fine.

Can anyone help me a bit with this please?

Thanks,

Nick


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>