• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

Script to change ad office attribute bulk

i am currently using the below

Import-Module Activedirectory
Import-csv c:\input.csv | %{Set-ADUser $_.User -Office $_.Office}

but i would also like to update the address field in a different script or combine them how would i go about this?
0
Jorge Ocampo
Asked:
Jorge Ocampo
  • 5
  • 4
1 Solution
 
oBdACommented:
The relevant attributes are -City -Country -POBox -PostalCode -State -StreetAddress
Just make sure that your csv contains the respective columns, and set the ones you need using the correct header names (the header names don't have to match the AD attribute names, but it's easier to handle):
Import-Module Activedirectory
Import-csv c:\input.csv | % {Set-ADUser $_.User -Office $_.Office -City $_.City -StreetAddress $_.StreetAddress}

Open in new window

0
 
Jorge OcampoAuthor Commented:
@oBdA for the streetaddresss do you know how i can create the following

1254 german blvd
STE 125A

instead of 1254 german blvd STE 125A
0
 
oBdACommented:
Basically by adding <backtick>r<backtick>n where you want the line break.
Set-ADUser -Identity jdoe -StreetAddress "1254 german blvd`r`nSTE 125A"

Open in new window

The question is how your csv file is created and whether line breaks are supported. Powershell doesn't mind during import, as long as the fields are properly enclosed in double quotes. If not, you need some other "line break" character (let's say '|'), which will then be replaced during import:
Set-ADUser $_.User -StreetAddress $_.StreetAddress.Replace('|', "`r`n")

Open in new window

0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Todd NelsonSystems EngineerCommented:
Assuming this question is related to your other question (https://www.experts-exchange.com/questions/28976541/Script-to-change-ad-office-attribute-bulk.html).

If you can, create a single CSV file.  If you need to get information from AD to create appropriate headers for a CSV, run this command (for a single user...

Get-ADUser -Identity "USERNAME" -Properties *

Open in new window


NOTE: There is a assumption you are running these commands from PowerShell on a DC and have already run the command to import the AD module (Import-Module ActiveDirectory).

Review the parameters provided on the left of the screen.  Those can essentially be your headers for the CSV.  Given that, these are the headers you could use.  But like OBDA said, they do not have to match.

  • StreetAddress
  • POBox
  • City
  • State
  • PostalCode
  • Country

A sample CSV could look like this...

Name,Office,StreetAddress,POBox,City,State,PostalCode,Country
User1,Bakersfield,123 Street Lane,,Bakersfield,CA,12345,United States
User2,Barstow,234 Street Ave,,Barstow,CA,23456,United States

Open in new window


At this point, you can use the same CSV file in a single script or multiple scripts (as requested).

If a single script outside of the prior one you have then it will look something like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -StreetAddress $_.StreetAddress -POBox $_.POBox -City $_.City -State $_.State -PostalCode $_.PostalCode -Country $_.Country }

Open in new window


But you can remove any of the parameters and CSV mapping you don't need.  For example, if you don't want to edit/add P.O. Box or Country then remove them from the command, like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode }

Open in new window


If you want them combined with your prior script, just add the Office parameter and the CSV map back into the command, like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -Office $_.Office -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode }

Open in new window


Hoe that helps.
0
 
Jorge OcampoAuthor Commented:
the csv i am doing is comma delimited trying tried the first example and no go
0
 
oBdACommented:
The "`r`n" can't be part of the string in the csv; that was just an example for a PS string containing line breaks.
In the csv, there would have to be a real Chr(13)Chr(10) combination for this to work without a replacement character:
"User","StreetAddress"
"jdoe","1254 german blvd
STE 125A"

Open in new window

If this can't be done, decide on a character that's not used otherwise, and use the replace method above.
0
 
Jorge OcampoAuthor Commented:
gotcha thanks :)
0
 
Jorge OcampoAuthor Commented:
one last question any idea why i am only getting the first result written to the csv? if i do command without export i get all results but when i add export only write out first result and stops.

$users = Get-Content C:\Users\user\Desktop\move.txt

foreach ($user in $users){
    $SplitName = -split $user
    Get-ADUser -Filter "(GivenName -eq '$($SplitName[0])') -and (Surname -eq '$($splitName[1])')" |Select-Object name, samaccountname | Export-CSV C:\Users\USER\Desktop\FILE.csv -NoTypeInformation
}
0
 
oBdACommented:
Because you're exporting inside the loop. Since PS 3.0, Export-Csv supports -Append, but that's not really fast.
You can solve that with a subexpression:
$users = Get-Content C:\Users\user\Desktop\move.txt

$(foreach ($user in $users){
	$SplitName = -split $user
	Get-ADUser -Filter "(GivenName -eq '$($SplitName[0])') -and (Surname -eq '$($splitName[1])')" |
		Select-Object name, samaccountname
}) | Export-CSV C:\Users\USER\Desktop\FILE.csv -NoTypeInformation

Open in new window

0
 
Jorge OcampoAuthor Commented:
Amazing Help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now