ACTIVE DIRECTORY

I have got a list of 20 users. where there country location is blank.

I need to run a simple script on DC where i can update their country location as United States
pramod1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aravind ancheWindows/Vmware Commented:
I used the script in the below link before to update attributes

https://gallery.technet.microsoft.com/scriptcenter/Update-Active-Directory-2e3718ae

change the details in users,csv and run the batch file and select the attributes you want to change
0
PberSolutions ArchitectCommented:
You can also di it in the GUI:  
Select all 10 users (Shift click, or CTRL click),
Right click then select properties
Select Address Tab
Click the Country/Region checkbox
Select US for the dropdown.
0
pramod1Author Commented:
how about doing 500 users?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

PberSolutions ArchitectCommented:
A script as mentioned above would be better, however you could still do it in the GUI:

Create a Saved query with the following search filter:

        (&(objectCategory=user)(objectClass=user)(!co=US))

Then save it, select the query and let it populate.
The select CTRL-A and same process as my first comment.
0
PberSolutions ArchitectCommented:
The above query will look for all users that don't have US as their country and only display those users.  You can target the query and any OU you require or at the root if that is appropriate.  Let me know if you need help creating the query or I could give you the query XML to import.
0
pramod1Author Commented:
I need help in creating the query as I need to run powershell in AD DC
0
PberSolutions ArchitectCommented:
You can use the same filter I used in your script.    Did you need me to write you a script.... give me a few minutes
0
pramod1Author Commented:
ok
0
PberSolutions ArchitectCommented:
Import-Module ActiveDirectory

Get-ADUser -SearchBase 'ou=yourOu,dc=domain,dc=com' -Filter "(&(objectCategory=user)(objectClass=user)(!co=US))" |

   Foreach-Object {

    Set-ADUser -identity $_ -Country "US"    }

Open in new window


Just replace your ou and domain name as required
0
pramod1Author Commented:
how can we use CSV HERE
0
pramod1Author Commented:
AS I need to put 20 users in it
0
pramod1Author Commented:
$users = Import-Csv 
foreach Get-ADUser -SearchBase 'ou=yourOu,dc=domain,dc=com' -Filter "(&(objectCategory=user)(objectClass=user)(!co=US))" |

   Foreach-Object {

    Set-ADUser -identity $_ -Country "US"    }
($user in $users) {
0
pramod1Author Commented:
is this correct syntex
0
pramod1Author Commented:
can I omit OU as OU is not mentioned in the list
0
PberSolutions ArchitectCommented:
$users = Import-Csv -Path "C:\yourpath\userlist.csv"            
            
foreach ($user in $users) {            
 Set-ADUser -identity $user.SamAccountName -Country "US" 
}

Open in new window


Try this instead.  Adjust the path as required
0
PberSolutions ArchitectCommented:
As for your previous comment on the omitting the OU.  Yes.  You can target the searchbase to the root of your domain.

So if your domain is called:  Mydomain.Local
The searchbase would be: 'dc=Mydomain,dc=local"

But if you really want to use a input file, then the import csv script should work.
0
PberSolutions ArchitectCommented:
Sorry wrote that off the cuff during a meeting, just tried that script.  Didn't work.  A few tweaks

$users = Import-Csv -Path "C:\yourpath\userlist.csv"                      
foreach ($user in $users) {            
Set-ADUser $user.SamAccountName -Replace @{c="us"} 
}

Open in new window


This script assumes that userlist.csv has a column titled SamAccountName
0
pramod1Author Commented:
thanks a lot , I will attach the excel sheet, please confirm which columns needs to be in CSV.

I will attach shortly, thanks for all input.
0
pramod1Author Commented:
I am attaching the excel sheet, which are the columns I should use from this excel sheet in to csv.
test.xlsx
0
pramod1Author Commented:
$users = Import-Csv -Path "C:\yourpath\userlist.csv"                      
foreach ($user in $users) {            
Set-ADUser $user.SamAccountName -Replace @{c="us"}
}

Is above script different from below as below script worked by my ad engineer
# Import AD Module            
Import-Module ActiveDirectory            
write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $userscsv                  
$users = Import-Csv -Path C:\temp\users.csv            
# Loop through CSV and update users if the exist in CVS file            
           
foreach ($user in $users) {            
#Search in specified OU and Update existing attributes            
 Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=an,DC=xx,DC=com" |            
  Set-ADUser -replace @{c = "$($user.c)"}
 Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=an,DC=xx,DC=com" |
  Set-ADUser -replace @{co = "$($user.co)"}            
}
Write-Host 'done!' –
0
PberSolutions ArchitectCommented:
The column is blank, but the column "Login Name" looks like the one you need.  That would likely be the userid they use the login to the domain right?  

So in my script the $user.SamAccountName, "SamAccountName" is the column you are looking for from your CSV.

So you have a few choices, the simplest would be to rename the "Login Name" to "SamAccountName".  Then the script will enumerate through all the users within that column.
0
PberSolutions ArchitectCommented:
Alternately you could change the script to user the Login Name column, but it contains a space and you would need to remove the space so it would be LoginName and do the same on your spreadsheet.

The code would look like this now:

$users = Import-Csv -Path "C:\yourpath\userlist.csv"                      
foreach ($user in $users) {            
Set-ADUser $user.LoginName -Replace @{c="us"} 
}

Open in new window

0
PberSolutions ArchitectCommented:
Regarding your AD Engineer's script.  It basically does the same thing.  That script will run through the SamAccountName column in users.csv file just like mine, but will query for each users before attempting to set attributes.  Mine doesn't query for the user first, it just attempts to set the attribute because it got the samaccountname already from the users.csv.  That script will also get the "c"  and "co" information from columns within the users.csv.  Mine just sets it to "US".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pramod1Author Commented:
what column should I take from excel sheet , my last question
0
PberSolutions ArchitectCommented:
column for what?

What script are you using, mine or your AD Engineer's?

Your AD Engineer's script requires three columns:
  1. It is looking for a column called "SamAccountName" and it wants userids in that column.
  2. A second called called "c" and it wants the two letter country code.  In your case: "US"
  3. A Third called called "co" and it wants the two letter country code.  In your case: "US"

My script only requires one column:
  1. It is looking for a column called "SamAccountName" and it wants userids in that column.

This is part of the reason why my script is so much smaller.  It just needs the userid to know who to edit, then it just sets the country code to US.
0
pramod1Author Commented:
I am running the same script for LA but users are from argentina, brazil, chile so what should I put in c and co field
0
PberSolutions ArchitectCommented:
So if you are using your engineers table, then you would need to reference this table:
https://msdn.microsoft.com/en-us/library/ms707477(v=vs.85).aspx

So for the c and co, use the same code.

So
CL for Chili
AR for Argentina
BR for Brazil
0
PberSolutions ArchitectCommented:
0
pramod1Author Commented:
so for ex. c will be cl
and co chile?
0
pramod1Author Commented:
So both in C and co FILELD I should put CL for chile
0
PberSolutions ArchitectCommented:
Sorry,

c is the country code
co is the full name

co = Chile
c - CL
0
pramod1Author Commented:
Get-ADUser : The supplied distinguishedName must belong to one of the following partition(s):
'CN=Configuration,DC=xxx,DC=com , CN=Schema,CN=Configuration,DC=xxx,DC=com , DC=ForestDnsZones,DC=xxx,DC=com ,
At C:\temp\chile-Update-AD-attribute-counrty-from-CSV.ps1:12 char:2
0
pramod1Author Commented:
I am getting above error
0
PberSolutions ArchitectCommented:
on this line:

Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=an,DC=xx,DC=com" | 

did you replace dc=an, dc=xx, dc=com with your actual domain?

i.e if your domain is an.yourco.com, then it would be:
dc=an,dc=yourco,dc=com
(Please don't post our actual domain)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.