Compare CSV to AD to determine if former employee accounts are still active

I need to compare a CSV file containing employees who have left the company to AD to ensure their accounts have been disabled.  If the account is still active it needs to be disabled and moved to an OU name Departed.  Is there a way to do this using PowerShell?
BudAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Of course there is a way to this using PowerShell.
This assumes the csv has a header line and a column "SamAccountName".
$departedOU = 'OU=Departed,DC=contoso,DC=com'
Import-Csv -Path C:\temp\users.csv | ForEach-Object {
	"Processing '$($_.SamAccountName)' ..." | Write-Host
	If ($ADUser = Get-ADUser -Filter "samAccountName -eq '$($_.SamAccountName)'") {
		If ($ADUser.Enabled) {
			"  - enabled, will disable ..." | Write-Host -NoNewline
			Try {
				' OK' | Write-Host -ForegroundColor Green
				Set-ADUser -Identity $ADUser.DistinguishedName -Enabled $false -ErrorAction Stop
			} Catch {
				" $($_.Exception.Message)" | Write-Host -ForegroundColor Red
			}
		} Else {
			"  - already disabled." | Write-Host
		}
		$parentOU = $ADUser.DistinguishedName -replace '\ACN=.*?,(?=OU=)'
		If ($parentOU -ne $departedOU) {
			"  - currently in OU '$($parentOU)', will move ..." | Write-Host -NoNewline
			Try {
				Move-ADObject -Identity $ADUser.DistinguishedName -TargetPath $departedOU -ErrorAction Stop
				' OK' | Write-Host -ForegroundColor Green
			} Catch {
				" $($_.Exception.Message)" | Write-Host -ForegroundColor Red
			}
		} Else {
			"  - already in OU '$($departedOU)'." | Write-Host
		}
	} Else {
		"User '$($_.SamAccountName)' not found!" | Write-Warning
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BudAuthor Commented:
Wow!  That was fast.  And it works great!!  Is it possible to write all the responses generated by the script to a text file for later review?
oBdACommented:
Easiest way is to use a transcript:
Start-Transcript -Path C:\Temp\Whatever.log
## Script from above ...
Stop-Transcript

Open in new window

BudAuthor Commented:
Thank you oBdA!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.