Updating Office location +1000 users

Hi, I need to update office locations for approx 1000 users, where the office field in AD has typo’s, should be something else etc etc. I want to script it, which should only be about 30 lines, but i’ve attached the first line as I can’t test due to live environment. Was hoping someone could verify if this is correct?

Get-ADUser -filter 'physicaldeliveryofficename -eq "Asdhtown"'| Set-ADUser -physicaldeliveryofficename "Ashtown"

Use the -WhatIf parameter. It will tell you what it will try to do without executing. I do not think -PhysicalDeliveryOfficeName is a parameter in the Set-ADUser. Cmdlet. You should be able to use -Office

pwshliquori

Just tested on one user and it seems it should be:

Get-ADUser -filter 'physicaldeliveryofficename -eq "Asdhtown"'| Set-ADUser -office "Ashtown"
Is there a way I can get an output of how many are changed? Rather than it just commiting and having to check AD? - thanks

[quote quote=149028]Just tested on one user and it seems it should be:

Get-ADUser -filter 'physicaldeliveryofficename -eq "Asdhtown"'| Set-ADUser -office "Ashtown"
Is there a way I can get an output of how many are changed? Rather than it just commiting and having to check AD? – thanks

[/quote]

 

Yes Set-aduser has a -whatif switch so you can see what it will do

and if you want a count just use a foreach loop with an count in it

I sometimes, like to do it in a “stupid way” so i will out put each of the username and dump this in the text file, but this, if you want to execute it.

-whatif will be the perfect way to do gauging.

 

When you are ready to perform this, you can use the -Verbose or -Passthru parameters to show the output. The -Passthru parameter only has a limited amount of properties that can be shown and unfortunately Office is not one.

Example with -Verbose parameter:

[pre]
VERBOSE: Performing the operation “Set” on target “CN=Test User,OU=Users,DC=example,DC=com”.
[/pre]

pwshliquori

just a really fast stab at this, also including error handling

[pre]
$userstoupdate = Get-ADUser -filter ‘physicaldeliveryofficename -eq “Asdhtown”’ -properties physicaldeliveryofficename,displayname
foreach ($user in $userstoupdate)
{
try
{
Set-ADUser $user.samaccountname -physicaldeliveryofficename “Ashtown”
“$($user.displayname) office updated to Ashtown”|out-file updatelog.txt -append
}
catch
{
“ERROR updating office for $($user.displayname)”|out-file updatelog.txt -append
}
}
notepad.exe updatelog.txt
[/pre]

I don’t think there’s a -physicaldeliveryofficename parameter. You have to use the -replace parameter. Maybe test it on one user.

Get-ADUser -filter 'physicaldeliveryofficename -eq "Asdhtown"' -ResultSetSize 1 | 
  Set-ADUser -replace @{physicaldeliveryofficename='Ashtown'} -whatif

Btw setting the editmode to emacs makes it nice to list all the parameters at once by pressing tab.

PS C:\Users\js> Set-PSReadlineOption -EditMode Emacs
PS C:\Users\js> set-aduser -P
Partition                             POBox                                 PipelineVariable
PassThru                              PostalCode                            pv
PasswordNeverExpires                  PrincipalsAllowedToDelegateToAccount
PasswordNotRequired                   ProfilePath

good catch JS, just change in the set-aduser portion, to office