Thursday, January 5, 2017

Delete Multiple AD users (List of users with SamAccountName are in csv file ) using below Powershell script.



Import-Csv C:\temp\Del.csv | Foreach-Object {Remove-ADUser -Identity $_.SamAccountName -Confirm:$False }

Restore AD user with SamAccountName or Display name and Restore AD OU..



// Restore AD object by Displayname
Get-Adobject -filter {displayname -eq 'Abc xyz'} -IncludeDeletedObjects | restore-Adobject


// Shows all the Deleted parents and child
Get-Adobject -filter {isDeleted -eq $true -and name -ne 'Deleted Objects'} -includeDeletedObjects -property * | ft msds-lastKnownRDN,lastKnownParents -auto -wrap


// Restore AD OU

Get-ADObject -Filter {msds-lastKnownRDN -eq ' OU name '} -IncludeDeletedObjects | Restore-ADObject


//Restore an object from a filtered list of users
This command finds a deleted user whose SAM account name is pattifuller and restores it.

Windows PowerShell
PS C:\> Get-ADObject -Filter 'samaccountname -eq "pattifuller"' -IncludeDeletedObjects | Restore-ADObject

Note: Forest and Domain function level should be 2008R2  and above, Recycle bin feature should be enable then only the above command will work.

Reference Link.
https://technet.microsoft.com/en-us/library/hh852318(v=wps.630).aspx

Move list of AD users to a particular OU



# Import AD Module
import-module ActiveDirectory

# Import CSV
$MoveList = Import-Csv -Path "C:\temp\users.csv"
# Specify target OU.This is where users will be moved.
$TargetOU =  "OU=Disabled Accounts,DC=us,DC=Microsoft,DC=com"
# Import the data from CSV file and assign it to variable
$Imported_csv = Import-Csv -Path "C:\temp\users.csv"

$Imported_csv | ForEach-Object {
     # Retrieve DN of User.
     $UserDN  = (Get-ADUser -Identity $_.SamAccountName).distinguishedName
     Write-Host " Moving Accounts ..... "
     # Move user to target OU.
     Move-ADObject  -Identity $UserDN  -TargetPath $TargetOU
     
 }
 Write-Host " Completed move "
 $total = ($MoveList).count
 Write-Host "  $total accounts have been moved succesfully..."

Disable list of AD Users from a CSV file.



get-content C:\temp\users.csv |get-aduser |set-aduser -enabled $false