PS script to read a file and move objects.

Hi,

We urgently need a PS script to read a file that contains list of samaccountids and distinguished names.  

The file looks like the following:

Tsmith     ou=sales,ou=ny,dc=mydomain,dc=com
asterling     ou=sales,ou=la,dc=mydomain,dc=com
wadam     cn=users,dc=mydomain,dc=com

we want the script to read the first column which is the user login id and move it to the distinguished name in the second column.  

Please help.

Thanks.
nav2567Asked:
Who is Participating?
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.

nav2567Author Commented:
My file looks something like this:

cn=Thomas,Smith,ou=myOU,dc=mydomain,dc=com     ou=sales,ou=ny,dc=mydomain,dc=com
cn=Alice,Sterling,ou=myOU,dc=mydomain,dc=com        ou=sales,ou=la,dc=mydomain,dc=com
cn=William,Adam,ou=myOU,dc=mydomain,dc=com      cn=users,dc=mydomain,dc=com

The first column is the source and second column is the destination.  

Please advise.
0
nav2567Author Commented:
I have tried to follow the below links and my script throws errors:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Windows/Q_27786646.html

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/dd9821b9-9c49-4d93-b5af-c79f927d724c/move-users-to-a-new-ou-getting-information-from-csv-file?forum=ITCG

Someone please advise.  

I need to Create a PS script to read csv file with samaccountids, DIFFERENT destination OUs,  and move objects

Thanks.
0
Will SzymkowskiSenior Solution ArchitectCommented:
You can use the below script to accomplish this...
Import-module activedirectory
$Data = Import-csv "c:\filename.csv"
ForEach ($User in $Date) 
{
$User.samaccountname
$User.distinguishedName
Get-ADUser -Identity -Identity $User.samaccountname -Properties DistinguishedName
Move-ADObject -Identity $_.DistinguishedName -TargetPath $User.DistinguishedName
}

Open in new window


Will.
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nav2567Author Commented:
Thanks.

So, should I put my filename.csv like this?  What should I put in the fist row?  samaccountname   DistinguishedName

Tsmith     ou=sales,ou=ny,dc=mydomain,dc=com
asterling     ou=sales,ou=la,dc=mydomain,dc=com
wadam     cn=users,dc=mydomain,dc=com

or like this?  What should I put in the first row?

cn=Thomas,Smith,ou=myOU,dc=mydomain,dc=com     ou=sales,ou=ny,dc=mydomain,dc=com
cn=Alice,Sterling,ou=myOU,dc=mydomain,dc=com        ou=sales,ou=la,dc=mydomain,dc=com
cn=William,Adam,ou=myOU,dc=mydomain,dc=com      cn=users,dc=mydomain,dc=com

Also, the loop statement ForEach ($User in $Date), is it supposed to be ForEach ($User in $Data)?

Line 7 has two "Identity" also.  Is that intended?
0
Will SzymkowskiSenior Solution ArchitectCommented:
Sorry typed it too fast. I have corrected all of the above you have mentioned.

Import-module activedirectory
$Data = Import-csv "c:\filename.csv"
ForEach ($User in $Data) 
{
$User.samaccountname
$User.distinguishedName
Get-ADUser -Identity $User.samaccountname -Properties DistinguishedName
Move-ADObject -Identity $_.DistinguishedName -TargetPath $User.DistinguishedName
}

Open in new window


Construct your CSV like you first example. Make sure that you have column headings like below...
samaccountname                    DistinguishedName
Tsmith                                             ou=sales,ou=ny,dc=mydomain,dc=com
asterling                                          ou=sales,ou=la,dc=mydomain,dc=com

Will.
0
nav2567Author Commented:
Thanks.
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.