locked
Powershell Script to Retrieve AD User, Group, Group Members Info RRS feed

  • Question

  • Good morning and Hi to all!

    I am a newbie to Powershell scripting and have a task on hand but unable to get the results I needed and hope I can get some help here.

    There are many Groups in AD, but I need to focus only on 2 distinct Groups namely TeamA and TeamB. I would like to query AD for the group the current logon user belongs to, showing only the SAMAccountNane of the Group.

    Further to this, I would be required to check whether the current logon user SAMAccountName is in the same Group as that of another SAMAccountName.

    I will provide a more detailed scenario if required.

    Really appreciate any guidance as to how I can achieve the above.

    Thank you and best regards.
    Ronald

    Monday, August 17, 2020 12:19 AM

All replies

  • I am really a newbie in scripting and very bad at it. I would like to share with you more regarding my requirements so that you will understand what I'm trying to achieve and provide me your kind guidance and expertise in achieving it.

    Below are the Powershell scripts I have at this point of time;

     1. Get logon user
              > $env:username
    Ronald

     2. List Groups in AD beginning with "Temp"
              Get-ADGroup -Filter "name -like 'Temp*'" | select name
    TempA
    TempB

     3. Get list of Groups user is member of
              (Get-ADUser $env:username -Properties MemberOf | Select MemberOf).MemberOf | % {$_.Split(",")[0].Replace("CN=","")}
    Group Policy Creator Owners
    Enterprise Admins
    Schema Admins
    Domain Admins
    Administrators
    TempA

     From here, I need to create a script to identify currently logon username, followed by the group (TempA or TempB) it belongs to.
    I hope these info will help.
    Thank you.
    Ronald
    Monday, August 17, 2020 1:11 AM
  • Hi,

    Regarding your question, asking this on our new home on Microsoft Q&A Powershell forum would yield better info.

    https://docs.microsoft.com/en-us/answers/topics/windows-server-powershell.html

    --------------------------
    "System Center" forum will be migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the "System Center" forum's new home on Microsoft Q&A!

    Tuesday, August 18, 2020 7:23 AM
  • Hi,

    just put your parts together:

    [array]$UserGroups = (Get-ADUser $env:username -Properties MemberOf | Select MemberOf).MemberOf
    [array]$TempGroups =  (Get-ADGroup -Filter "name -like 'Temp*'").DistinguishedName 
    
    $TempGroups | where {$UserGroups -contains $_} | % {$_.Split(",")[0].Replace("CN=","")}

    Regards,

    Stefan


    More and news about System Center at stillcool.de and sc-orchestartor.eu .

    Wednesday, August 19, 2020 7:42 AM
    Answerer