Share via

Need help in script

Ramanjaneyulu Butharaju 421 Reputation points
2021-09-17T12:00:45.493+00:00

Hello All,

I'm trying to move my user accounts from one OU to another OUs

So I have list of 2300 + users need to move to different OUs.

Lets say I have 100 users who are in different OUs of same domain now I want all these 100 users to move new OU

I have the list of all users with thier samaccountnames and emails.

users.txt

user1

user2

user3

.

.

user100

Now i placed this txt file as

$path = C:\users.txt

Now I want each of user in this txt file search across the domain and select that user and move to new "OU" by using his identity "samaccountname"

I'm able to move the move the object by using this script

Move-ADObject -Identity 'CN=David Smith,CN=Users,DC=ad,DC=contoso,DC=com' -TargetPath 'OU=Accounts,OU=Sensitive,DC=ad,DC=contoso,DC=com'

Instead of "CN" i want to use SamAccount or email to move all users to new OU.

Can anyone help me with how the user in txt will be search across the domain and moved to new OU ??

Thanks,

Ram

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2021-09-17T19:22:55.55+00:00

    If you can place the NAME of the target OU into each file as the 1st line (and the samAccountNames comprising the remainder of the file), then you can do this all in one small script:

    $files = "users1-100.txt","users101-200.txt","users201-300.txt"
    $files |
        ForEach-Object{
            # get just the 1st line of the input file (the name of the target OU)
            $targetouname = Get-Content $_ |
                        Select-Object -First 1
            $oudn = (Get-ADOrganizationalUnit -Filter "Name -eq '$targetouname'").distinguishedName
            if ($oudn.length -gt 0){
                Get-Content $_ |
                    Select-Object -Skip 1 |     # skip the name of the target OU and just get users
                        ForEach-Object{
                            $u = $_.Trim()      # remove any leading/trailing whitespace
                            if ($u.length -gt 0){
                                Try{
                                    Get-AdUser -Identity $u -ErrorAction STOP                       # verify the user exists
                                    Move-ADObject -Identity $u -TargetPath $oudn -ErrorAction STOP  # move to target OU
                                }
                                Catch{
                                    Write-Host "User $u not found in AD or Move-ADObject failed: $_"
                                }
                            }
                        }
            }
            Else{
                Write-Host "OrganizationalUnit $targetouname was not found"
            }
        }
    

    Was this answer helpful?

    0 comments No comments

  2. Limitless Technology 40,106 Reputation points
    2021-09-17T14:34:04.56+00:00

    Hello Ram,

    I would create the file with Get-ADUser and SamAccountName:

    import-Module activeDirectory
    $Users = Get-ADUser -Filter * | Select-Object -Property Name,SamAccountName;
    $USers | Export-Csv -Path ".\Users.csv" -Delimiter ';' -NoTypeInformation;

    or by Name email

    Get-ADUser -Filter * -Properties DisplayName, EmailAddress, Title | select DisplayName, EmailAddress | Export-CSV C:\Scripts\Email_Addresses.csv

    Best regards,

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.