How to Get User belong to different group in Different domain along with passing authentication using powershell script?

463 Views Asked by At

How to Get User belong to different group in Different domain along with passing authentication using PowerShell script?

Should get different groups in different domain also how to pass authentication in order to get those groups whether user belong to those group or not

im using below command to get user belong to group.

Get-ADUser -Identity "userID" -Properties MemberOf

by this command gets current domain user i need user list of all domain user list and its group

1

There are 1 best solutions below

0
Daredevil On

To retrieve group memberships for a user across different domain, you'll have to connect to domain's Active Directory separately, and then query for the user's group memberships in that domain.

# Prompt for credentials
$credential = Get-Credential

# UserID to search for
$userID = 'yourUserID' 

$domain = 'yourdomain.com'

# retrieve group memberships
Write-Host "Checking $domain"
   
# Connect to the domain's AD
$domainContext = New-PSDrive -Name AD_$domain -PSProvider ActiveDirectory -Root "" -Scope Global -Server $domain -Credential $credential
        
# Check user's group membership in the domain
$user = Get-ADUser -Identity $userID -Properties MemberOf -Server $domain -Credential $credential

$groups = ($user.MemberOf | ForEach-Object { (Get-ADGroup -Identity $_ -Server $domain).Name }) -join ', '

Write-Host "User $userID is a member of the following groups in $domain: $groups"