I am a fairly new PS user ... Looking for some help with the powershell script to get a list of security groups the user is a member of.
To describe what I need:
- I have an input list (txt file) with many users (samaccountnames). Each name is on a new line.
- I need a script to search for these names in AD - the whole forest, not just one domain
- the output should look like "samaccountname" and a list of groups in which this account is a member of one line, so I can sort it in excel
This is the script I have:
$users = Get-Content C:\users.txt ForEach ($User in $users) { $getmembership = Get-ADUser $User.Users -Properties MemberOf | Select -ExpandProperty memberof $getmembership | Out-File -Append c:\membership.txt }
but this causes me an error:
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again. At line:4 char:28 + $getmembership = Get-ADUser <<<< $User.Users -Properties MemberOf | Select -ExpandProperty memberof + CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Anyway, this script will not search the whole forest.
Example input list:
username1 username2 username3 username4... etc
Results List Example
username1;group1;group2;group3 username2;group1;group2;group3;group4... etc or something similar
Any help would be greatly appreciated.
powershell active-directory membership
Martin_K
source share