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.

4 Answers 4

15

Try this:

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

Sign up to request clarification or add additional context in comments.

1 Comment

You need to read about powershell piping: technet.microsoft.com/en-us/library/ee176927.aspx
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.

Comments

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

2 Comments

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 Review
Sorry, I thought that it could be useful for the user to understand why the command didn't work. Thanks for the clarification anyway
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.