Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 267
elock37 Avatar asked Oct 03 '12 16:10

elock37


People also ask

What is the PowerShell command to move to another directory?

Cmdlet. Move-Item cmdlet is used to move a directory by passing the path of the directory to be moved and destination path where the folder is to be moved.


1 Answers

Try this:

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

like image 179
CB. Avatar answered Oct 10 '22 00:10

CB.