Posted in
Hosting & IIS7,
Windows Powershell,
Windows Server |
No Comment | 5,484 views | 02/03/2010 12:06
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
} |
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" |
Check-ADUser -Username "yusufozturk"
You can use this function with Status property. It’s useful.