Checking Active Directory User with Powershell

This is another way to check active directory user with Powershell. I made a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Function Check-ADUser
{
Param ($Username)
 
    $ADRoot = [ADSI]''
    $ADSearch = New-Object System.DirectoryServices.DirectorySearcher($ADRoot) 
    $SAMAccountName = "$Username"
    $ADSearch.Filter = "(&(objectClass=user)(sAMAccountName=$SAMAccountName))"
    $Result = $ADSearch.FindAll()
 
    If($Result.Count -eq 0)
    {
        Write-Host "No such user on the Server" | Out-Null
        $Status = "0"
    }
    Else
    {
        Write-Host "User exist on the Server" | Out-Null
        $Status = "1"
    }
 
    $Results = New-Object Psobject
    $Results | Add-Member Noteproperty Status $Status
    Write-Output $Results    
}

Usage:

Check-ADUser -Username "yusufozturk"

You can use this function with Status property. It’s useful.

Tags: , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply