We help IT Professionals succeed at work.

diabale and move account in AD via powershell

SEHC
SEHC asked
on
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
Comment
Watch Question

Raheman M. AbdulMessaging and Directory Services

Commented:
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. AbdulMessaging and Directory Services

Commented:
$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 SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
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.
SEHCAdmin

Author

Commented:
Hi Will

can you send me the columns for the CSV?
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

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

Will.
SEHCAdmin

Author

Commented:
yes please I would like to do this for multiple users
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
I will modify the script and post back shortly.

Will.
SEHCAdmin

Author

Commented:
thanks Will
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
What info does your CSV file currently have?

Do you only have the EmployeeID?

Will.
Senior Solution Architect
Most Valuable Expert 2015
Top Expert 2015
Commented:
I have modified the script based on you only having the EmployeeID info. If you have additional info in your CSV please let me know and i will add the changes.

Import-module activedirectory
$OU = "ou=domain users,dc=example,dc=com"
$UserList = Import-Csv "c:\filename.csv"
ForEach ($User in $UserList) {
$User.EmployeeID
Get-ADUser -Filter { EmployeeID -eq $User.EmployeeID } -Properties EmployeeID | Disable-ADAccount | 
Move-ADObject -Identity $User.distinguishedname -TargetPath $OU
}

Open in new window


Will.
SEHCAdmin

Author

Commented:
there is no files at the moment.
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
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.
SEHCAdmin

Author

Commented:
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