Import to Active Directory Users from PowerShell

Another quick script to Import data from a CSV back in to Active Directory on a domain controller

# Import AD Module             
Import-Module ActiveDirectory 

#Get Admin accountb credential

$GetAdminact = Get-Credential
$DomainOU = GET-ADDomain | Select-Object -ExpandProperty UsersContainer

#Store the CSV in a table
$Users = Import-CSV c:\temp\ExportADUsers\Update.csv

foreach ($User in $Users)
{
           
#Search in specified OU and Update existing attributes            
 Get-ADUser -Filter "SamAccountName -eq '$($User.'Logon Name')'" -Properties * -SearchBase $DomainOU | Set-ADUser -GivenName $($User.'First Name') -Surname $($User.'Last Name') -DisplayName $($User.'Display Name') -Title $($User.'Directorate') -Description $($User.'Directorate') 

if (($User.'Account Status' -eq 'Disabled') ) {
#Search in specified OU and disabled accounts as required
 Get-ADUser -Filter "SamAccountName -eq '$($User.'Logon Name')'" -Properties * -SearchBase $DomainOU | Disable-ADAccount
 }

}

"Done"

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.