Share via


Get-ADGroupMember Find Specific User

Question

Tuesday, March 15, 2016 7:06 AM

Hey Guys,

I am not sure what would be the best way to go about locating specific user from Get-ADGroupMember cmdlet and alerting user.

eg:

$account = Read-Host "Please Enter Account"
$security = Read-Host "Please Enter Security Group"
Get-ADGroupMember -Identity $security  | select name,samaccountname

How would I go about letting user know if $account's samaccountname is ADGroupMemeber?

Would I have to make an empty hash table @{ but how do i insert ; output from ADGroupMemeber}  and run a loop?

All replies (3)

Tuesday, March 15, 2016 7:29 AM ✅Answered

Hi Azazel,

welcome to Technet.

You can do this with "if" and "Where-Object":

$account = Read-Host "Please Enter Account"
$security = Read-Host "Please Enter Security Group"
if ($usr = Get-ADGroupMember -Identity $security | Where-Object { $_.name -eq $account } )
{
    Write-Output "User $($usr.name) ($($usr.DistinguishedName)) is member of group $($security)"
}

Cheers,
Fred

There's no place like 127.0.0.1


Tuesday, March 15, 2016 10:36 AM ✅Answered

You're welcome. Here's how:

$array = @()
$hash = @{}

$account = Read-Host "Please Enter Account"
$security = Read-Host "Please Enter Security Group"
if ($usr = Get-ADGroupMember -Identity $security | Where-Object { $_.name -eq $account } )
{
    Write-Host "User $($usr.name) ($($usr.DistinguishedName)) is member of group $($security)"
    $array += $usr
    $hash[$usr.Name] = $usr
}

$array
$hash

There's no place like 127.0.0.1


Tuesday, March 15, 2016 10:10 AM

thanks Fred!! worked like a charm.

Out of pure curiosity how would i go about inserting an output of a command in to an array or a hash table?

Get-ADGroupMember -Identity $security