Avatar of SEHC
SEHC
Flag for Canada asked on

diabale and move account in AD via powershell

Hi

I would like to dis able a user account in ad and move them to a different OU.  the thing is I would like to do it via there employee ID number.  this way I can be 100% the I have the right person.

does anyone have a script like that or know of one that I can take a look at?

thanks

SEHC
PowershellActive Directory

Avatar of undefined
Last Comment
SEHC

8/22/2022 - Mon
Raheman M. Abdul

If you could have the SAM account name it can be done. No idea if EmployeeID is related with sam account name or login name how it is setup in AD.
Can you let us know this please.
Raheman M. Abdul

$samAccountName = "AbdulRaheman"
Disable-ADAccount -Identity $samAccountName 

Get-ADUser -Identity $samAccountName | Move-ADObject -TargetPath 'OU=Destination,OU=CEApps,DC=domain,Dc=com'

Open in new window

Will Szymkowski

Try the following code below...
Import-module activedirectory
$User = "username"
$OU = "ou=domain users,dc=example,dc=com"
Get-ADUser -Filter { EmployeeID -eq $User.EmployeeID } -Properties EmployeeID | Disable-ADAccount | Move-ADObject -Identity $User.distinguishedname -TargetPath $OU

Open in new window


Will.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SEHC

ASKER
Hi Will

can you send me the columns for the CSV?
Will Szymkowski

You want to do this for multiple users that are in a csv file?

Will.
SEHC

ASKER
yes please I would like to do this for multiple users
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Will Szymkowski

I will modify the script and post back shortly.

Will.
SEHC

ASKER
thanks Will
Will Szymkowski

What info does your CSV file currently have?

Do you only have the EmployeeID?

Will.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Will Szymkowski

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SEHC

ASKER
there is no files at the moment.
Will Szymkowski

So, based on the script above that i have modified you need to construct your CSV like below...
EmployeeID
012331
123150
995844
etc

Or whatever values you have listed in the EmployeeID attribute need to be under the heading EmployeeID in the CSV file.

Will.
SEHC

ASKER
HI


I am getting the following error

Move-ADObject : Cannot validate argument on parameter 'Identity'. The argument
is null. Supply a non-null argument and try the command again.
At C:\Scripts\AD Powershell scripts\Disable account employee ID\adempdis.ps1:6
char:24
+ Move-ADObject -Identity <<<<  $User.distinguishedname -TargetPath $OU
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindi
   ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.MoveADObject
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.