Posted in
Windows Powershell,
Windows Server |
1 Comment | 2,743 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
} |
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 |
$Status = (Check-ADUser -UserID ysfozy).Status
This script can search user from Active Directory and show you results.