search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Checking Active Directory User with Powershell
Posted in Windows Powershell, Windows Server | 1 Comment | 2,627 views | 16/08/2009 20:16

I needed to check an user from Active Directory with Powershell so I wrote this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Check-ADUser
{
param ($UserID)
 
    $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
    $Searcher.Filter = "(&(objectClass=user)(sAMAccountName= $UserID))"
    $CheckADUser = ($Searcher.Findall()).Count
 
    If($CheckADUser -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 Sample:

$Status = (Check-ADUser -UserID ysfozy).Status

This script can search user from Active Directory and show you results.


Comments (1)

Paul Theelen

October 4th, 2011
15:11:17

Hi,

I implemented this into my powershell script that removes folder but i have one question.
What happens if AD would be unavailable? If it removes all my folders, then i would be in quite a bit of trouble.

Kind Regards,

Paul Theelen



Leave a Reply