• Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
Tuesday, November 17, 2026
No Result
View All Result
Subscribe Now
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Trump Administration Revokes Pay Increase for Head Start Educators

    Trump Administration Revokes Pay Increase for Head Start Educators

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
The Insight Post
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Trump Administration Revokes Pay Increase for Head Start Educators

    Trump Administration Revokes Pay Increase for Head Start Educators

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
No Result
View All Result
No Result
View All Result
Home Business Asia

Learn to monitor group memberships with PowerShell

by Theinsightpost
August 30, 2022
in Business Asia
0 0
0
Learn to monitor group memberships with PowerShell


Controlling memberships in privileged groups is an important task that PowerShell automation can handle with minimal effort.

The easy approach to manage members of groups, such as domain admins in Active Directory or the local administrators group on a server, is to control group management access — and keep your fingers crossed that no one goes rogue. However, in larger environments where many people have administrative access to these groups, finger crossing isn’t a good policy. With PowerShell, we can easily write a script to monitor, or even enforce, the memberships of these groups.

For both Windows servers and Active Directory, the approach to manage these groups will be similar. First, we need to maintain a list of the groups to monitor, then get the members of each group and finally check for any changes since the last time the script ran.

How to gather local group memberships on Windows Server

On a local server, the list of groups is likely quite short. For this article, we will work with just two groups — administrators and remote desktop users — but you can expand your coverage to other groups by adjusting the script accordingly.

Start the script with a variable to hold the groups and then add a foreach loop to go through each of those groups and return the group membership.

$groups = @(
    'Administrators'
    'Remote Desktop Users'
)
$groupMembers = foreach ($group in $groups) {
    Get-LocalGroupMember $group
}

Now we can look at the output of $groupMembers and see the members of each of those groups (Figure 1).

server group membership
Figure 1. The results from the group membership collection script on the server lists the local administrators.

If you wanted to take those memberships and send them as an emailed report, you could. But we can use PowerShell to monitor group memberships and send notifications if any changes occur.

To start, export the memberships to a CSV file and then load that to compare the previous run with the current run. To do this, use two additional foreach loops: the first will look for users that were added and the second will look for users that were removed.

foreach ($group in $groups) {
    $previousRunMemberships = $null
    if (Test-Path "C:tmp$group.csv") {
        $previousRunMemberships = Import-Csv -Path "C:tmp$group.csv"
    }
    $groupMembers = Get-LocalGroupMember $group
    $added = foreach ($member in $groupMembers) {
        if ($previousRunMemberships.SID -notcontains $member.SID) {
            $member
        }
    }
    $removed = foreach ($member in $previousRunMemberships) {
        if ($groupMembers.SID -notcontains $member.SID) {
            $member
        }
    }
   $groupMembers | Export-Csv -Path "C:tmp$group.csv" -NoTypeInformation
}

The script will output the current users to a CSV file that will be used to compare current group memberships to a previous state.

The next step adds the notification. There are many options, such as the Send-MailMessage command, but this tutorial will use Azure Logic Apps to route your notification.

First, make a function and include it in the top of the script, as follows:

Function Send-LogicAppEmail {
    param (
        [string]$LogicAppUri = '<logic app uri>',
        [string]$To = '[email protected]',
        [string]$CC,
        [string]$Subject,
        [string]$Message
    )
    $headers = @{
        'Content-Type' = 'application/json'
    }
    $body = @{
        To = $To
        CC = $CC
        Subject = $Subject
        Body = $Message
    }
    $splat = @{
        Uri = $LogicAppUri
        Method = 'POST'
        Headers = $headers
        Body = ($body | ConvertTo-Json)
    }
    Invoke-RestMethod @splat
}

Next, build the HTML and call that function in the foreach loop.

    if ($added -or $removed) {
        $html = @"
<h1>$($env:COMPUTERNAME)</h1>
<h2>$group</h2>
<h3>Added</h3>
<pre>
$($added | Format-Table | Out-String)
</pre>
<h3>Removed</h3>
<pre>
$($removed | Format-Table | Out-String)
</pre>
"@
        Send-LogicAppEmail -To '[email protected]' -Subject "Changes to $group on $($env:COMPUTERNAME)" -Message $html
    }

Figure 2 shows an example of the email that indicates a user was added to the remote desktop users group.

email notification
Figure 2. The notification shows the list of users who were added or removed from the remote desktop users group.

What follows is the full group membership monitoring script.

Function Send-LogicAppEmail {
    param (
        [string]$LogicAppUri = '<logic app uri>',
        [string]$To = '[email protected]',
        [string]$CC,
        [string]$Subject,
        [string]$Message
    )
    $headers = @{
        'Content-Type' = 'application/json'
    }
    $body = @{
        To = $To
        CC = $CC
        Subject = $Subject
        Body = $Message
    }
    $splat = @{
        Uri = $LogicAppUri
        Method = 'POST'
        Headers = $headers
        Body = ($body | ConvertTo-Json)
    }
    Invoke-RestMethod @splat
}
$groups = @(
    'Administrators'
    'Remote Desktop Users'
)
foreach ($group in $groups) {
    $previousRunMemberships = $null
    if (Test-Path "C:tmp$group.csv") {
        $previousRunMemberships = Import-Csv -Path "C:tmp$group.csv"
    }
    $groupMembers = Get-LocalGroupMember $group
    $added = foreach ($member in $groupMembers) {
        if ($previousRunMemberships.SID -notcontains $member.SID) {
            $member
        }
    }
    $removed = foreach ($member in $previousRunMemberships) {
        if ($groupMembers.SID -notcontains $member.SID) {
            $member
        }
    }
    if ($added -or $removed) {
        $html = @"
<h1>$($env:COMPUTERNAME)</h1>
<h2>$group</h2>
<h3>Added</h3>
<pre>
$($added | Format-Table | Out-String)
</pre>
<h3>Removed</h3>
<pre>
$($removed | Format-Table | Out-String)
</pre>
"@
        Send-LogicAppEmail -To '[email protected]' -Subject "Changes to $group on $($env:COMPUTERNAME)" -Message $html
    } 
$groupMembers | Export-Csv -Path "C:tmp$group.csv" -NoTypeInformation
}

Take this script, save it in on a local path on each server, and then call it periodically with a scheduled task.

How to monitor group memberships in Active Directory

The process to monitor groups in Active Directory is similar to the steps to monitor local groups on server systems. The only difference is we will use commands from the Active Directory module, which will require the following script to run either on a domain controller or on a device with the Active Directory module installed that is connected to the domain controller.

First, set the groups array to reference Active Directory groups. Add as many as you want, but for this example we’ll use the following two groups:

$groups = @(
    'Domain Admins'
    'Schema Admins'
)

Use the Get-ADGroupMember command to gather group members.

$groupMembers = Get-ADGroupMember $group

Lastly, adjust the HTML message and subject to reference Active Directory.

    if ($added -or $removed) {
        $html = @"
<h1>Active Directory</h1>
<h2>$group</h2>
<h3>Added</h3>
<pre>
$($added | Format-Table | Out-String)
</pre>
<h3>Removed</h3>
<pre>
$($removed | Format-Table | Out-String)
</pre>
"@
        Send-LogicAppEmail -To '[email protected]' -Subject "Changes to $group in Active Directory" -Message $html
    }

Now the full script will look like the following:

Function Send-LogicAppEmail {
    param (
        [string]$LogicAppUri = '<logic app uri>',
        [string]$To = '[email protected]',
        [string]$CC,
        [string]$Subject,
        [string]$Message
    )
    $headers = @{
        'Content-Type' = 'application/json'
    }
    $body = @{
        To = $To
        CC = $CC
        Subject = $Subject
        Body = $Message
    }
   $splat = @{
        Uri = $LogicAppUri
        Method = 'POST'
        Headers = $headers
        Body = ($body | ConvertTo-Json)
    }
    Invoke-RestMethod @splat
}
$groups = @(
    'Domain Admins'
    'Schema Admins'
)
foreach ($group in $groups) {
    $previousRunMemberships = $null
    if (Test-Path "C:tmp$group.csv") {
        $previousRunMemberships = Import-Csv -Path "C:tmp$group.csv"
    }
    $groupMembers = Get-ADGroupMember $group
    $added = foreach ($member in $groupMembers) {
        if ($previousRunMemberships.SID -notcontains $member.SID) {
            $member
        }
    }
    $removed = foreach ($member in $previousRunMemberships) {
        if ($groupMembers.SID -notcontains $member.SID) {
            $member
        }
    }
    if ($added -or $removed) {
        $html = @"
<h1>Active Directory</h1>
<h2>$group</h2>
<h3>Added</h3>
<pre>
$($added | Format-Table | Out-String)
</pre>
<h3>Removed</h3>
<pre>
$($removed | Format-Table | Out-String)
</pre>
"@
        Send-LogicAppEmail -To '[email protected]' -Subject "Changes to $group in Active Directory" -Message $html
    }
    $groupMembers | Export-Csv -Path "C:tmp$group.csv" -NoTypeInformation
}

Figure 3 shows the email notification generated by the script.

email notification Active Directory change
Figure 3. The email notification shows the name of the user who was added to the domain admins group in Active Directory.

The email shows that a user with the name Test User was added to the domain admins group.

This script can also run periodically via the Task Scheduler.

Why monitor group memberships with a PowerShell script?

PowerShell might not have the allure of a sophisticated monitoring product, but it certainly can meet your needs if you take the time to learn how to write and maintain a script. A more sophisticated version of the script can go even further and remove users that should not be in the group or add users who have been removed accidentally. PowerShell is flexible enough to handle these tasks and let you focus on other work.



Source link

ShareTweetSend
Previous Post

Retirees struggling to get by living off New Zealand super

Next Post

Germany Fights Digital Backwardness | CEPA

Related News

Business Asia

Tokayev Proposes Dipping Into National Fund to Turn Kazakhstan into a ‘Big Construction Site’ – The Diplomat

October 29, 2026
Business Asia

China Is Learning to Sell Japanese Culture Better Than Japan – The Diplomat

October 28, 2026
Business Asia

Indonesia Announces New Measures to Improve Stock Market Transparency – The Diplomat

October 27, 2026
Business Asia

Indonesia Reduces Gas Supply to Singapore, Citing Domestic Energy Needs – The Diplomat

October 26, 2026
Next Post
Germany Fights Digital Backwardness | CEPA

Germany Fights Digital Backwardness | CEPA

Discussion about this post

Subscribe To Our Newsletters

    Customer Support


    1251 Wilcrest Drive
    Houston, Texas
    77042 USA
    Call-832.795.1420
    e-mail – news@theinsightpost.com

    Subscribe To Our Newsletters

      Categories

      • Africa
      • Africa-East
      • African Sports
      • American Sports
      • Arts
      • Asia
      • Australia
      • Business
      • Business Asia
      • Business- Africa
      • Canada
      • Defense
      • Education
      • Egypt
      • Energy
      • Entertainment
      • Europe
      • European Soccer
      • Finance
      • Germany
      • Ghana
      • Health
      • Insight
      • International
      • Investing
      • Japan
      • Latest Headlines
      • Life & Living
      • Markets
      • Mobile
      • Movies
      • New Zealand
      • Nigeria
      • Politics
      • Scholarships
      • Science
      • South Africa
      • South America
      • Sports
      • Tech
      • Travel
      • Travel-Africa
      • UK
      • USA
      • Weather
      • World
      No Result
      View All Result

      Recent News

      Gators Shock Wildcats 21-14 in Thrilling Upset Amid Rollercoaster Season

      Gators Shock Wildcats 21-14 in Thrilling Upset Amid Rollercoaster Season

      November 15, 2026
      Government Announces Historic  Billion Investment in Energy Infrastructure Development

      Government Announces Historic $2 Billion Investment in Energy Infrastructure Development

      November 15, 2026
      Notre Dame Postgame Insights: Dive into Superchats with Westwood One Sports

      Notre Dame Postgame Insights: Dive into Superchats with Westwood One Sports

      November 15, 2026
      Transform Your Laptop Setup with These 5 Affordable Accessories

      Transform Your Laptop Setup with These 5 Affordable Accessories

      November 15, 2026
      • Home
      • Advertise With Us
      • About Us
      • Corporate
      • Consumer Rewards
      • Forum
      • Privacy Policy
      • Social Trends

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      Welcome Back!

      Login to your account below

      Forgotten Password?

      Retrieve your password

      Please enter your username or email address to reset your password.

      Log In

      Add New Playlist

      We are using cookies to give you the best experience on our website.

      You can find out more about which cookies we are using or switch them off in .

      No Result
      View All Result
      • Home
      • Insight
      • Blog
      • Business
      • Entertainment
      • Health
      • Politics
      • Shop
        • Gift Shop
        • Value Shop
        • Store
        • Bargain Shop
        • Discount
      • Sports
      • Tech
      • Travel
      • USA
      • Video
      • World
        • Asia
        • Africa
        • South America
        • North America
        • Europe
        • Oceania

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      The Insight Post
      Powered by  GDPR Cookie Compliance
      Privacy Overview

      This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

      Strictly Necessary Cookies

      Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

      Cookie Policy

      More information about our Cookie Policy