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?

[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.

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

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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
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
Active Directory

From novice to tech pro — start learning today.