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

I need a little help. I have little PowerShell experience, but I am working with the Pocket Guide on my side and with my GoogleFu.

Currently my plan is to request a username and save it, use Get-ADUser with the saved username to get and save DistinguishedName, use Move-ADObject to move the user from DistinguishedName to the target path.

The problem I am facing is storing and calling these things. I have this that gives me user information. How can I highlight only the distinguished name and save it?

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

After saving the DN, can Move-ADObject use the stored value? I tried to save individual values, for example:

 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" because it does not use the stored value.

+4
source share
3 answers

Try the following:

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

+11
source

Just aside from this -

Powershell cannot recognize variables such as $name when they are enclosed in single quotes, since the shell processes values ​​such as literals. Use double quotes to work with variables:

Eg. write-host '$name' will give the result $name , but write-host "$name" will 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 like expected. On the other hand, you will study more interesting materials using the conveyor.

+4
source

For your personal interest only:

CB., With the conveyor passing the object. You, instead of using a string, should use the object with the command:

 Move-ADObject 
0
source

Source: https://habr.com/ru/post/1437633/


All Articles