• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Scripts / PowerShell List all Users and Group Membership

PowerShell List all Users and Group Membership

06/01/2018 by Stephanos 1 Comment

PowerShell List all Users and Group Membership

Scenario:

PowerShell List all Users and Group Membership

In an environment with a lot of user and groups, it is very difficult to keep track of the groups that each user is a member.Have you ever thought to check the group and their member and clarify that only the intended user are members for each group? My case when I have created the below script, was to find out that users in a specific location are under the correct groups only. So I needed a list to check the groups.

The below script connects to Active directory and get a list of all users for a specific location (ex. Cyprus Office). Then the script check the membership of each user and gather all the information in a report and sends it by email. If there be an error or exception during the run then the script will provide me with the error or list of errors if they exist.

You can download the script here or copy it from below.

Related Links:

  • PowerShell Scripts
  • PowerShell Tutorials
  • PowerShell Users and Membership – Part 2
  • Encrypt password with key using PowerShell
  • Send Email using PowerShell
  • Email error variable in PowerShell
  • Import-Module – Microsoft Docs
  • Get-Content – Microsoft Docs
  • ConvertTo-SecureString – Microsoft Docs
  • New-Object – Microsoft Docs
  • Get-ADUser – Microsoft TechNet
  • Get-ADPrincipalGroupMembership – Microsoft TechNet
  • Send-MailMessage – Microsoft Docs

Solution / Script:

import-module ActiveDirectory
cd ad:
$File = "C:\Scripts\Password.txt"
$Key = (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,26,27,28,29,30,31,32)
$EmailUser = "Script-User@domain.com"
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$EmailCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $EmailUser,$Password
$To = 'User1@domain.com'
$From = 'Script-User@domain.com'
$CyprusUsers = Get-ADUser -Filter * -SearchBase "OU=Employees,OU=Cyprus,DC=domain,DC=com"
$EmailResult = ""
$ErrorEmailTemp = ""
Foreach ($CyprusUser in $CyprusUsers) {
    $CyprusGroups = Get-adprincipalgroupmembership $CyprusUser.SamAccountName | select Name
    $CyprusUserName = $CyprusUser.Name
    $CyprusGroupsName = $CyprusGroups.Name
    $CyprusPrincipalName = $CyprusUser.UserPrincipalName
    
    $EmailTemp = @"
    <tr>
        <td class="colorm">$CyprusUserName</td>
        <td>$CyprusGroupsName</td>
    </tr>
"@
    $EmailResult = $EmailResult + "`r`n" + $EmailTemp
}
$EmailUp = @"
<style>
    body { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; color:#434242;}
    TABLE { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TR {border-width: 1px;padding: 10px;border-style: solid;border-color: white; }
    TD {font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;padding: 10px;border-style: solid;border-color: white; background-color:#C3DDDB;}
    .colorm {background-color:#58A09E; color:white;}
    .colort{background-color:#58A09E; padding:20px; color:white; font-weight:bold;}
    .colorn{background-color:transparent;}
</style>
<body>
    <h3>Script has been completed successfully</h3>
    
    <h4>Users and their MemberOf Attribute have been exported.</h4>
    
    <table>
        <tr>
            <td class="colort">User</td>
            <td class="colort">Groups</td>
        </tr>
"@
$EmailDown = @"
</table>
</body>
"@
$Email = $EmailUp + $EmailResult + $EmailDown
    
if ($EmailResult -ne "") {
    send-mailmessage `
        -To $To `
        -Subject "Users and their Groups Cyprus Report $(Get-Date -format dd/MM/yyyy)" `
        -Body $Email `
        -BodyAsHtml `
        -Priority high `
        -UseSsl `
        -Port 587 `
        -SmtpServer 'smtp.office365.com' `
        -From $From `
        -Credential $EmailCredentials
}
if ($error -ne $null) {
    foreach ($value in $error) {
    
        $ErrorEmailTemp = @"
        <tr>
            <td class="colorm">$value</td>
        </tr>
"@
        $ErrorEmailResult = $ErrorEmailResult + "`r`n" + $ErrorEmailTemp
    }
    
    $ErrorEmailUp = @"
    <style>
        body { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; color:#434242;}
        TABLE { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
        TR {border-width: 1px;padding: 10px;border-style: solid;border-color: white; }
        TD {font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;padding: 10px;border-style: solid;border-color: white; background-color:#C3DDDB;}
        .colorm {background-color:#58A09E; color:white;}
        .colort{background-color:#58A09E; padding:20px; color:white; font-weight:bold;}
        .colorn{background-color:transparent;}
    </style>
    <body>
    
        <h3 style="color:#BD3337 !important;> WARNING!!!</h3>
        
        <p>There were errors during users check</p>
        
        <p>Please check the errors and act accordingly</p>
        
        <table>
"@
    $ErrorEmailDown = @"
    </table>
    </body>
"@
    $ErrorEmail = $ErrorEmailUp + $ErrorEmailResult + $ErrorEmailDown
    send-mailmessage `
        -To $To `
        -Subject "Users and their Groups Report $(Get-Date -format dd/MM/yyyy) - WARNING" `
        -Body $ErrorEmail `
        -BodyAsHtml `
        -Priority high `
        -UseSsl `
        -Port 587 `
        -SmtpServer 'smtp.office365.com' `
        -From $From `
        -Credential $EmailCredentials
}
Summary
PowerShell List all Users and Group Membership
Article Name
PowerShell List all Users and Group Membership
Description
PowerShell List all Users and Group Membership. By using this script you will receive a report of your users and the groups each user is in.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Scripts Tagged With: Arithmetic Operators, Comparison Operators, ConvertTo-SecureString, Get-ADPrincipalGroupMembership, Get-ADUser, Get-Content, Get-Date, Import-Module, New-Object, PowerShell ForEach, Select-Object, Send-MailMessage

Reader Interactions

Trackbacks

  1. PowerShell List all Users and Group Membership – Stephanos Constantinou – Hari Babu Online says:
    16/03/2018 at 01:55

    […] https://www.sconstantinou.com/powershell-list-all-users-and-group-membership/ […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact