Wednesday, 24 August 2016

Search for user existence in a domain - Powershell

Validate User existence in a domain using text file input: 


Last week, I had received request to migrate some list of users using ADMT tool, but I know some users in the list were already migrated because few names which I looked in the file are familiar to me...

List contains around 50 users and I don't have time to validate each and every user in a domain for their account existence. Hence I remember the try catch method which I used earlier for checking the AD user account existence.

Note: The input file contains the SAMACCOUNTNAME of the objects. Just input those samaccoutname into the notepad text file and saved with "userscheck.txt" and save the input file to c:\temp folder

Solution:

Please find below the script content.. This time I have added the cmdlets to generate CSV file based on the validation.

# Script Contents --- Script shared with no warranties.. it was developed based on our test environment. Please read carefully, modify and use it as required .

#********************************************************************************

# Setting Default location to c:\temp Directory and Getting contents into variable

Set-Location 'C:\temp'
$data=Get-content '.\userscheck.txt'

# Preparing the CSV file output formats and path

$logpath="c:\temp"
$logdate = (Get-Date -Format ddMMyyyy)
$logfilename= $logdate+ ' ' + ‘User-checking’+".csv"
$out= $logpath + '\' + $logfilename

Import-Module Activedirectory

Add-content $out -value “Userid,Exist”

foreach ($user in $data)

{

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

        Add-Content $out -Value "$User,YES"
    }
catch
    {
        Add-Content $out -Value "$user,NO"
    }
   
}

#*******************************************************************************






No comments:

Post a Comment