Set-ADUser script help

by Loomer at 2012-11-19 19:35:49


New to powershell and scripting but have been learning as I go and now have task to perform at work to update AD info for all users. Is working on test box as I’d hoped but want to neaten it up in two ways if possible and running into brick walls. Any help or advice welcome! :slight_smile:

1. Want to add a line to bottom of setad*.txt that I output catch error that says "This many accounts were not found (x)" and have function that counts x

2. Repeat the above for the try section and have ‘{0} has been updated in AD’ -f $user.SamAccountName and pipe to same text file as above also with text of count.

Code below:



# Import AD Module

Import-Module ActiveDirectory

# Create Date variable for file naming

$date = (Get-Date).ToString(‘dd-MM-yyyy-HHmmss’)

# Import CSV in variable $USERS. CSV location and name must exist as in script.

$USERS = Import-CSV c:\Users\users.csv

# Loop through CSV and update users if exist or return not found in AD to console and file

foreach ($user in $USERS){

try {

Set-ADUser -Identity $user.sAMAccountname -City $user.City -Department $user.Department -Office $user.Office -OfficePhone $user.OfficePhone -Company $user.Company -Title $user.Title


catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {

‘{0} does not exist in AD’ -f $user.SamAccountName | % {write-host $_ ; Add-Content -path C:\Users\setad_$date.txt -value $_} }

by nohandle at 2012-11-20 03:37:56
I have already responded on Powershell com forum. :slight_smile:
by DonJ at 2012-11-20 07:55:11
(FYI, you can use the Code or PowerShell toolbar buttons here to get formatted code)

Ugh, Write-Host. Spew. Sure you don’t mean Write-Warning? Write-Error?

Anyway, it looks like you know the code to use - what’s this not doing that you’d like it to do?
by Loomer at 2012-11-20 18:30:26
On steep learning curve. Only halfway through your book :slight_smile:

I was asked by boss if there was a way to go through all of AD and update peoples details so had 3 line code which just imported CSV then cycled through doing Set-ADUser. Worked fine except threw error for user not found on my test system as only loaded a couple of examples.

Boss is a coder but not PS and wanted me to put in Try/Catch with screen output and write to file for user not found. Had to ask around for info especially stuff like [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] for the catch and advice I got from PS IRC to output to screen and file was code I used as worked. I think I was trying to use Tee-Object but that wouldn’t append and -append only available in v3? and needs to be run on v2.

Wanted to add User added on screen and sent to same text file in the try block and also add counter and some text in text file saying how many of success and fail but flailing.

nohandle gave me some tips on another site which I need to try out.
by DonJ at 2012-11-21 07:15:19
So, a catch doesn’t actually need the exception.

try {
Set-ADUser -Identity whatever -Department IT -ErrorAction Stop - ErrorVariable MyError
} catch {
"I caught $MyError" | Out-File c:\errors.txt -Append
Write-Warning "Error: $MyError"

If you can point to one thing (to start with) that you want to do, but don’t know how, it’ll be easier to focus. We could then move on to the next thing.