Add a suffix in the Name attribut of an account range

This topic contains 17 replies, has 3 voices, and was last updated by  Simon B 3 weeks, 6 days ago.

  • Author
    Posts
  • #80263

    Marti
    Participant

    Hi,

    I'm new with the Powershell langage but I'm in a company who needs a lot of informations. So I have to learn more about it.

    I have an Active Directory and I have to modify the "name" attribut of a range of them.
    I used a command to get a list in .Csv format of these account and I want to add the suffixe "_OUT" at the end of all of them.

    My Csv file has some attribut like "name","sameaccountname"...

    For example, the account "Toto Charles", I want a script to add _OUT so the result "Toto Charles_OUT"

    I don't know which command can I use, and how can I do this in a CSV file.

    Someone can help me for the beginning ?

    Thanks and sorry for my english.

  • #80266

    Sam Boutros
    Participant

    To read a CSV file, use the Import-Csv cmdlet as in

    $myData = Import-Csv .\myfile.csv
    

    To add text to a field, use something like

    $myData | foreach { $_.Name = "$($_.Name)_Out" }
    # This adds '_Out' at the end of the 'Name' field..
    

    Finally write it back to the CSV file

    Export-Csv .\myfile.csv -NoType
    
  • #80269

    Simon B
    Participant

    not sure if I follow but you could do something like

    $users = get-aduser -identity "Toto Charles" | select name, samaccountname # or whatever your search criteria is
    foreach ($user in $users)
    {
    $user.name := $user.name + "_OUT"
    }
    $users | export-csv c:\names.csv

  • #80300

    Marti
    Participant

    Hi,

    First, thanks for your answer both of you.

    I tried the solution of Sam Boutros and it works perfectly.

    Now, I have a second question and maybe the answer of Simon B will help me.

    I have an Organisation Unit named "To Delete". And I want to do the modification with the "_OUT" for all of the users in this OU.
    We can take name of OU for example: OU=TEST DC=test.local
    How can I implement the modification of the name for the entire OU. Is it better if :
    -I get back the entire OU in a csv file, do the modification and put it back to the Active Directory

    -I create directly a request for the Active Directory to modify.

    Thanks for your time and skills.

  • #80303

    Simon B
    Participant

    you can add a searchbase to your ad query

    Get-AdUser -SearchBase "OU=TEST DC=test.local"
    Just user the -whatif parameter to test what will be changed before you run it live

  • #80306

    Marti
    Participant

    Yes but what next ? When I use the Get-Aduser command, how can I modify them ? with or without the csv, with which command because Get-AdUser is a request command.

    Have I to put a pipeline and put another command next ?

    Thanks

  • #80309

    Simon B
    Participant

    you could do something like (not tested)

    Get-ADUser -SearchBase "OU=TEST DC=test.local" -Filter * -Properties name | % {Set-ADUser -Identity $_.name ($_.name + "_Out") } -whatif

  • #80311

    Marti
    Participant

    I tried to do the beginning of the script.

    I have something like that :

    Import-Module ActiveDirectory
    
    #Create a CSV of the concerned OU with the requested parameters
    Get-AdUser -SearchBase "OU=TEST, DC=TEST, DC=local" -filter {SamAccountName -like "*"} -properties name|
    Select-Object name, samaccountname, distinguishedname |
    Export-csv C:\Users\A003634\Desktop\OU_Test_Base.csv
    
    #Importer the created csv into the variable
    $MonFichier = C:\Users\A003634\Desktop\OU_Test_Base.csv
    
    #Add the "_OUT" suffix to the name*
    #(I have a problem with my IF, It doesn't works I don't know why. I want to verify if the "_OUT" is already wrote, do nothing)
    $MonFichier | foreach {
    if ($MonFichier.Name -notmatch "_OUT"){
     $_.Name = "$($_.Name)_OUT" }
     elseif($MonFichier.Name -match "_OUT"){
        }
     }
     
    #Write back the result to a new csv file
    $MonFichier | Export-csv C:\Users\A003634\Desktop\OU_Test_OUT.csv -NoType
    
    #Import the new csv into a new variable
    $FichierModifie = Import-csv C:\Users\A003634\Desktop\OU_Test_OUT.csv
    
    foreach ($user in $FichierModifie){
    #Here i'm lost, i don't know what to do to edit edit the AD atttribut from the csv.
    }
    
  • #80312

    Simon B
    Participant

    you can do it in one line without csv files

    foreach ($user in (Get-ADUser -Filter { sAMAccountName -like '*' } -Properties Name -SearchBase "OU=TEST, DC=TEST, DC=local")) { $user.Name += "_OUT"; Set-ADUser -Instance $user } -whatif

  • #80318

    Marti
    Participant

    Yes, but Is it not clearer if I first put my Get-AD command on a csv, modify the csv with the "_OUT" and after that compare to the AD to copy the new name into. And I will prefer that option.

  • #80320

    Marti
    Participant

    I have my code like that :

    Import-Module ActiveDirectory
    
    #Create a CSV of the concerned OU with the requested parameters
    Get-AdUser -SearchBase "OU=TEST, DC=test, DC=local" -filter {SamAccountName -like "*"} -properties name|
    Select-Object name, samaccountname, distinguishedname |
    Export-csv C:\Users\A003634\Desktop\OU_Test_Base.csv
    
    #Importer the created csv into the variable
    $MonFichier = C:\Users\A003634\Desktop\OU_Test_Base.csv
    
    Add the "_OUT" suffix to the name
    $MonFichier | foreach {
    if ($MonFichier.Name -notmatch "_OUT"){
    $_.Name = "$($_.Name)_OUT" }
     elseif($MonFichier.Name -match "_OUT"){
        }
     }
     
    #Write back the result to a new csv file
    $MonFichier | Export-csv C:\Users\A003634\Desktop\OU_Test_OUT.csv -NoType
    
    $MonFichier | foreach {
    Set-ADUser -Identity $_.samaccountname | -name $_.name }
    

    I want to understand how the Set-ADUser works. And how can I fix my problem with the IF line 13

  • #80321

    Simon B
    Participant

    $users = Import-csv C:\Users\A003634\Desktop\OU_Test_OUT.csv

    foreach ($user in $users) {
    Get-ADUser -Filter * -Properties name -SearchBase "OU=TEST, DC=TEST, DC=local" |
    Set-ADUser -name $($user.name)
    }

    but you need to make sure that their names are unique, you will probably be better using SamAccountName

  • #80324

    Marti
    Participant

    What do you mean by unique ? Two people with the same name and surname ? Like 2 name called "Franck, Pierre" ?
    Because the SamAccountName is not visible when you look the OU, I want a visible modification

  • #80327

    Simon B
    Participant

    I mean 2 display names the same, so like you said 2 people called "Franck, Pierre

  • #80329

    Marti
    Participant

    It's ok, we use identification number for the dispay names in the Active Directory so we don't have same ID for two people.

    I have a last question about this script, why can't I make a IF fonction to not re write "_OUT" in my csv when it's already wrote. (line 11-15)

  • #80332

    Simon B
    Participant

    This seems to work

    $MonFichier = import-csv C:\test\addout.csv

    foreach ($name in $MonFichier){
    if ($name.Name -notmatch "_OUT"){
    $name.Name = $name.Name +"_OUT"}
    elseif($name.Name -match "_OUT"){
    }
    }

  • #80356

    Marti
    Participant

    It works,

    I will try this soon and come back to inform you if the script is OK.

    Thank you for your suggest .

  • #80359

    Simon B
    Participant

    Np anytime 🙂

You must be logged in to reply to this topic.