Moving users inside active directory using powerShell command

Good morning experts,

I am trying to learn all that I can PowerShell for active directory by asking questions since I can not find a book on the topic:

Presently, I need to know (in the simplest command possible) how to move one user from an Active Directory folder to another. Say I need to move a disabled user to a folder called DisabledUsers.

I looked for a "move" command but did not find one. Therefore, I am assuming that this has to be done in two stages, first copy the user to DisabledUsers and then remove him/her from wherever they are. Am I correct in this assumption?
Please advise with an example. Thanks.
adamtraskAsked:
Who is Participating?
 
footechCommented:
David's commands are for removing an account from a group and adding it to another.  If you're looking to move an account to another OU, which is what I read your question as asking, you will use the Move-ADObject command.  This command can be used to move different kinds of AD objects, not just users.   Here's an example usage, which will move the account with the distinguishedName "CN=Joe Blow,OU=Accounting,DC=Example,DC=com" (which you should be able to tell is in the Accounting OU), to an OU named "DisabledUsers".
Move-ADObject -Identity "CN=Joe Blow,OU=Accounting,DC=Example,DC=com" -TargetPath "OU=DisabledUsers,DC=Example,DC=com"

Open in new window

0
 
David Johnson, CD, MVPOwnerCommented:
$disabledusers = Search-ADAccount -AccountDisabled
foreach ($disableduser in $disabledusers) {
     if ( !($disableduser.SamAccountName -like "Guest") -and !($disableduser.SamAccountName -like "krbtgt")) 
        {
        write-host("Moving ",$disableduser.SamAccountName)
        Remove-AdGroupMember -identity "Users" -members $disableduser.SamAccountName 
        Add-ADGroupMember -Identity "DisabledUsers" -Members $disableduser.SamAccountName
        }
}

Open in new window

0
 
adamtraskAuthor Commented:
David, thank you very much for your response on a Sunday...

I am having difficulty understanding the job of the if statement in the foreach loop:

if ( !($disableduser.SamAccountName -like "Guest") -and !($disableduser.SamAccountName -like "krbtgt"))

Translating the above to plain English it tells me the following:  if the disabled user's samAccountName is not the same as "guest" and if that name is different from "krbtgt" then the moving process begins.

What does "Guest" and "krbtgt" represent... ?

Thanks
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
adamtraskAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for adamtrask's comment #a40694785

for the following reason:

Thank you very much. That's what I needed. I am sorry if my questions are not as succinct as they should be, but that is because I am a new hand trying to grasp too many things at the same time.
Thanks a lot
0
 
David Johnson, CD, MVPOwnerCommented:
Guest and krbtgt are system defined users default is set to disabled and should not be touched.
0
 
adamtraskAuthor Commented:
Thank you both... I am not sure what I did to generate the request to close the question
0
 
adamtraskAuthor Commented:
Thank you again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.