Solved

Script to change ad office attribute bulk

Posted on 2016-10-19
10
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 500 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 16

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

690 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