Tuesday, 25 April 2017

Search for User availability in domain using Powershell


Most of the time, we get request from the different teams to validate the list of domain accounts existance in domain and ask to provide the report. 

By using the POWERSHELL method we can easily can generate the report. The script may be looking to much of data but I tried to make it simple for beginners like me to understand it... :)

Here is the script content : 

# Before starting to use this script, please make notepad file with filename as Users.txt in   # the same directory where this powershell script is stored

# The input file should contain the user login name ( Samaccountname )

$logpath=".\"
$logdate = (Get-Date -Format ddMMyyyy)
$logfilename= $logdate+"User-check"+".log"
$log1=$logpath +"\" +$logfilename

Import-Module Activedirectory

# Simple method to test whether input file provided or not. If not provided, script will Exit.

if (Test-path -Path ".\users.txt")
{

Write-Host "Input file is valid"
}
else
{
Write-host "Input file is not avialable, Existing the Script"
EXIT
}

# Storing the "Users.txt" file contents to the variable called "data"

$data=Get-content '.\users.txt'

# Adding header information to the log file

Add-content $log1 -value "Samaccountname,Availability"


# Looping through the each account to variable called "User"

# Then using Try & Catch Method, script check for the account existance in Domain


foreach ($user in $data)
{

Try {
        Get-aduser $user -ErrorAction stop |out-null


        Add-Content $log1 -Value "$User,YES"
    }
    Catch {
        Add-Content $log1 -Value "$user,NO"
    }
  
  }

EXIT

# END Of Script ###########################################################


No comments:

Post a Comment