Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to change ad office attribute bulk

Posted on 2016-10-19
10
Medium Priority
?
80 Views
Last Modified: 2016-10-19
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
Comment
Question by:Jorge Ocampo
  • 5
  • 4
10 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41850203
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
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850270
@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
 
LVL 85

Expert Comment

by:oBdA
ID: 41850294
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 17

Expert Comment

by:Todd Nelson
ID: 41850296
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
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850309
the csv i am doing is comma delimited trying tried the first example and no go
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41850329
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
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850334
gotcha thanks :)
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850412
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
 
LVL 85

Expert Comment

by:oBdA
ID: 41850439
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
 
LVL 2

Author Closing Comment

by:Jorge Ocampo
ID: 41850907
Amazing Help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
This video discusses moving either the default database or any database to a new volume.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question