question

RamanjaneyuluButharaju-8253 avatar image
0 Votes"
RamanjaneyuluButharaju-8253 asked RichMatheisen-8856 edited

Need help in script

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

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"
         }
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.