Author Posts

September 20, 2017 at 3:24 pm

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.

September 20, 2017 at 3:40 pm

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

September 20, 2017 at 3:42 pm

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

September 21, 2017 at 8:48 am

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.

September 21, 2017 at 9:09 am

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

https://blogs.technet.microsoft.com/heyscriptingguy/2011/11/21/make-a-simple-change-to-powershell-to-prevent-accidents/

September 21, 2017 at 9:18 am

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

September 21, 2017 at 10:06 am

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

https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/18/use-powershell-to-set-ad-ds-users-display-names/

September 21, 2017 at 10:12 am

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

September 21, 2017 at 10:24 am

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

September 21, 2017 at 10:37 am

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.

September 21, 2017 at 10:47 am

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

September 21, 2017 at 10:49 am

$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

September 21, 2017 at 10:54 am

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

September 21, 2017 at 10:59 am

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

September 21, 2017 at 11:04 am

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)

September 21, 2017 at 11:39 am

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"){
}
}

September 21, 2017 at 2:18 pm

It works,

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

Thank you for your suggest .