Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to copy all users of OU "A" to the OU "B". My PowerShell shot at this is

$sourceEntry = [ADSI]"LDAP://OU=A,DC=demo,DC=com"
$targetEntry = [ADSI]"LDAP://OU=B,DC=demo,DC=com"

$searcher = New-Object DirectoryServices.DirectorySearcher($sourceEntry)
$searcher.Filter = "(objectClass=user)"
$results = $searcher.FindAll()

foreach($result in $results) {
    $user = $result.GetDirectoryEntry()
    $user.CopyTo($targetEntry)
}

My problem is, that $user appears to lack the CopyTo method I try to call. As far as I understand PowerShell, $user is an .NET object of the type System.DirectoryServices.DirectoryEntry ... in Visual Studio I find the method CopyTo ... in PowerShell I find none of it's methods, just properties.

I'm just starting with PowerShell, so please help!

share|improve this question
    
What do you mean by "copy"? You can't copy users around... are you trying to move them? –  Massimo Feb 24 '10 at 16:24

2 Answers 2

up vote 1 down vote accepted

According to this StackOverflow question, you should use the PSBase member of a DirectoryEntry object in order to access all of its methods. Try this:

foreach($result in $results) { 
    $user = $result.GetDirectoryEntry() 
    $user.PSBase.MoveTo($targetEntry) 
}
share|improve this answer
    
Ok, thanks, this works. Together with your first answer (You can't copy AD users) this is the answer I searched for. –  Hinek Mar 2 '10 at 11:47

You can't copy AD users.

You can move them from one place to another, or you can create new users based on existing ones... but in the latter case, you have to supply new user names, passwords and a few other things; it's not as simple as a "copy & paste" operation.

Users are security principals, they must be unique in a given domain; you can't have two "identical" user objects in different OUs.

share|improve this answer
    
Correct and Yup. In NDS (Novell Directory Services) you can have the same username in multiple OUs. Not in AD. –  mfinni Feb 24 '10 at 18:08
    
Ok, but the MoveTo method isn't accessible either ... –  Hinek Feb 26 '10 at 18:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.