6/3/14

Move list of user accounts to OU

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("UsersList.txt", ForReading) '--- LIST OF USERNAMES
Set objOU = GetObject("LDAP://OU=UserAccounts,DC=domain,DC=com") '--- ADD YOUR PATH TO THE OU
Const ForReading = 1
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close


For Each strLine in arrFileLines
strLDAP = GetDN(strLine)
objOU.MoveHere _
    "LDAP://" & strLDAP & "", vbNullString
Next


Function GetDN(UserID) 


      Set objConn = CreateObject("ADODB.Connection")
      objConn.Provider = "ADsDSOObject"
      objConn.Open "Active Directory Provider"
      
      Dim Base, Filter, Attr, Level, Server
      Server = "DC1" '--- NAME OF DOMAIN CONTROLLER
      
      Base = " & Server & "/DC=domain,DC=com>;" '--- ADD YOUR DOMAIN
      Filter = "(&(objectClass=user)(objectCategory=person)(samAccountName=" & UserID & "));"
      Attr = "distinguishedName;"
      Level = "SubTree"
      
      Set RecordSet = objConn.Execute(Base & Filter & Attr & Level)
      
      RecordSet.MoveFirst
      While Not RecordSet.EOF
            GetDN = RecordSet.Fields(0).Value
            RecordSet.MoveNext
      Wend 

End Function 

PowerShell to Disable users account & move them to different OU using CSV

########################################################################### 
# NAME:                Script to disable users account and move them to different OU using CSV 
# AUTHOR:              Imran Pathan 
# COMMENT:             All you need is fill up the Disable user account csv with saMAccountName & OU to move to.  
# VERSION HISTORY:     1.0 
# 1.0 12/19/2012 - Initial release 
############################################################################ 
$UsersToDisbableList=IMPORT-CSV C:\Imran\DisableUsers.csv  
$PrimaryDC = 'PDC002.yourdomain.local' 
$DomainName = 'yourdomain' 
Clear-Host 
Connect-QADService -service $PrimaryDC 
 
Function DisableUsers 
{ 
    Param( 
        [string] $_SamAccountName, 
        [string] $_MoveTOOU 
        ) 
    write-host ("User: $_SamAccountName") 
    Write-Host ("Move to OU: $_MoveTOOU") 
 
Get-ADUser $_SamAccountName | Set-ADUser -Enabled $false 
Start-Sleep -s 2 
Move-QADObject -Identity $_SamAccountName -NewParentContainer $_MoveTOOU 
Start-Sleep -s 2 
} 
 
FOREACH ($User in $UsersToDisbableList) { 
    DisableUsers $User.SamAccountName $User.MoveTOOU 
}