I have spent long hours getting this little script working. Many thanks to Olaf and Faris for getting me started. I did not know how to collect several properties and put them all back into one property so it could be written to, or Set, (Set-ADUser)
Everything below is sanitzed using obvious identical letters. JJJ ZZZ SSS etc.,
In this script I changed the DisplayName, combining samaccountname, l , Department, state
The Script removes the characters “JJJ” from the Department, if present.
And if extensionAttribute8 equals “Contractor” It inserts “(CTR)” after the last name.
And it took a long time to figure out how to take out a few characters of a Variable:
$department=$Userinfo.Department
$department = $department -replace “JJJJ-“, “”
CVS File:
GoodSamAcct
Lizzie.Borden
Jack.Ripper
Here’s what some of the properties look like for Lizzie and Jack before the script ran.
PS C:\windows\system32> Import-Csv -Path "C:\homeuse\Bulkusers2.csv" |
foreach {
Get-ADUser -Filter "cn -like '*$($_.GoodSamAcct)*'" -Properties * | select samaccountname, displayname, name, cn, mail, givenname, initials, sn, extensionAttribute8, ZZZZAbbreviation, Department, Description, l, state, distinguishedname, streetaddress, postalcode, ZZZZofficeid, personguid
}
samaccountname : Lizzie.Borden
displayname :
name : Lizzie.Borden
cn : Lizzie.Borden
mail : Lizzie.Borden@ZZZZ.www
givenname : Lizzie
initials :
sn : Borden
extensionAttribute8 : Contractor
ZZZZAbbreviation : JJJJ-SSSS
Department : JJJJ-SSSS
Description : Delete 02/27/2021 - RRR3925954 - JJJJ-SSSS
l : Twin Falls
state : ID
distinguishedname : CN=Lizzie.Borden,OU=Deprovision,OU=Users,OU=JJJJ,OU=Agencies,XX=ZZZZ,XX=net
streetaddress : 1441 FILLMORE ST
postalcode : 83301
ZZZZofficeid : 61056
personguid : D6407B64-A225-4C83-85CA-A84747A630E3
samaccountname : Jack.Ripper
displayname :
name : Jack.Ripper
cn : Jack.Ripper
mail : Jack.Ripper@tn.YYYY.net
givenname : Jack
initials :
sn : Ripper
extensionAttribute8 : Affiliate
ZZZZAbbreviation : SSSS-CD
Department : JJJJ-SSSS - JJJJ-SSSS-CD
Description : Delete 02/27/2021 - RRR3926269 - SSSS-CD
l : Selmer
state : TN
distinguishedname : CN=Jack.Ripper,OU=Deprovision,OU=Users,OU=JJJJ,OU=Agencies,XX=ZZZZ,XX=net
streetaddress : 512 Mulberry
postalcode : 38375
ZZZZofficeid : 102748
personguid : 4F2A4D20-3DF9-4F55-9B07-E9D03CCD3E28
Here is the script:
PS C:\windows\system32> Foreach ($user in $AllUsers) {
$Userinfo=Get-ADUser -Identity $user.GoodSamAcct -Properties department,state,l,extensionAttribute8
$firstname=$user.GoodSamAcct.Split(".")[0]
$lastname=$user.GoodSamAcct.Split(".")[1]
$department=$Userinfo.Department
$department = $department -replace “JJJJ-“, “”
$l=$Userinfo.l
$state=$Userinfo.state
$NewDispalyName=[string]::Concat($lastname,', ',$firstname,' - ',$department,', ',$l,', ',$state)
$InContractorDisplayName=[string]::Concat($lastname,', ',$firstname,' (CTR) - ',$department,', ',$l,', ',$state)
Write-Host $NewDispalyName
Write-Host $l
Write-Host $department
if ($Userinfo.extensionAttribute8 -eq "Contractor"){Set-ADUser -Identity $user.GoodSamAcct -DisplayName $InContractorDisplayName}
else{
Set-ADUser -Identity $user.GoodSamAcct -DisplayName $NewDispalyName
}
Get-ADUser -Identity $user.GoodSamAcct -Properties department,state,displayname | ft department,state,displayname
}
This is what appears on the Console after the script runs. :
Borden, Lizzie - SSSS, Twin Falls, ID
Twin Falls
SSSS
Department state displayname
---------- ----- -----------
JJJJ-SSSS ID Borden, Lizzie (CTR) - SSSS, Twin Falls, ID
Ripper, Jack - SSSS - SSSS-CD, Selmer, TN
Selmer
SSSS - SSSS-CD
department state displayname
---------- ----- -----------
JJJJ-SSSS - JJJJ-SSSS-CD TN Ripper, Jack - SSSS - SSSS-CD, Selmer, TN
PS C:\windows\system32>