How can I use PowerShell to move a user in AD?
Asked Answered
K

4

6

I'm in need of a little help. I've got little to no PowerShell experience but I'm working with a Pocket Guide by my side and my GoogleFu.

Currently, my plan is to prompt for a username and store it, use Get-ADUser with the stored username to get and store the DistinguishedName, use Move-ADObject to move the user from the DistinguishedName to the target path.

The problem I'm encountering is storing and calling these things. I have this, which gives me the info on a user. How can I isolate just the distinguished name and store it?

$name = read-host "Enter user name"
Get-ADUser $name

After storing the DN, can Move-ADObject use the stored value? I've attempted to store individual values like:

Move-ADobject 'CN=$name,OU=department,OU=company,DC=Domain,DC=net' -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net'

But this returns "Directory object not found" as it doesn't use the stored value.

Kelton answered 3/10, 2012 at 16:32 Comment(0)
C
15

Try this:

Get-ADUser $name| Move-ADObject -TargetPath 'OU=nonactive,OU=compny,DC=domain,Dc=net'

Cautery answered 3/10, 2012 at 16:59 Comment(1)
You need to read about powershell piping: technet.microsoft.com/en-us/library/ee176927.aspxCautery
O
7

Just an aside on this -

Powershell can't recognise variables such as $name when they're enclosed in single quotation marks as the shell treats such values as literal strings. Use double quotes to work with variables:

Eg. write-host '$name' would give the output $name, but write-host "$name" would return the value in the variable.

So Move-ADobject "CN=$name,OU=department,OU=company,DC=Domain,DC=net" -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net' should work as expected. On the other hand, you will learn more interesting stuff by using the pipeline.

Oblong answered 27/10, 2013 at 15:32 Comment(0)
G
0

Only for your personal interest:

CB., doing the pipeline is passing an object. You, instead of using a string, should use an object with the command:

Move-ADObject
Godspeed answered 8/3, 2018 at 14:41 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewTachograph
Sorry, I thought that it could be useful for the user to understand why the command didn't work. Thanks for the clarification anywayGodspeed
T
0

Move-ADobject "CN=$name,OU=department,OU=company,DC=Domain,DC=net" -TargetPath 'OU=NonActive,OU=company,DC=Domain,DC=net' -id 'username'

will do it

Teem answered 13/2, 2024 at 13:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.