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.
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.
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 ?
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.
}
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.
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
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