• 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
Thursday, August 20, 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
    “The End of Oak Street” does pastiche right

    “The End of Oak Street” does pastiche right

    NTSB releases new videos, images from deadly Philadelphia medical jet crash

    NTSB releases new videos, images from deadly Philadelphia medical jet crash

    Iran Calls Trump ‘Delusional’ After He Claims Hormuz a ‘New U.S. Territory’

    Iran Calls Trump ‘Delusional’ After He Claims Hormuz a ‘New U.S. Territory’

    IPOs for Open AI, Anthropic could unlock 0B for charity

    IPOs for Open AI, Anthropic could unlock $430B for charity

    Poll showing Bass leading Raman by double digits was bogus, company says

    Poll showing Bass leading Raman by double digits was bogus, company says

    Multiple people dead as flooding continues in Indiana : NPR

    Multiple people dead as flooding continues in Indiana : NPR

    Sophie Cunningham defends stance on biological men in women’s sports

    Sophie Cunningham defends stance on biological men in women’s sports

    Democrats Make South Carolina the First Contest in 2028 Presidential Primary Calendar

    Democrats Make South Carolina the First Contest in 2028 Presidential Primary Calendar

    Has the Left Really Met Its Limit? 

    Has the Left Really Met Its Limit? 

  • 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
    “The End of Oak Street” does pastiche right

    “The End of Oak Street” does pastiche right

    NTSB releases new videos, images from deadly Philadelphia medical jet crash

    NTSB releases new videos, images from deadly Philadelphia medical jet crash

    Iran Calls Trump ‘Delusional’ After He Claims Hormuz a ‘New U.S. Territory’

    Iran Calls Trump ‘Delusional’ After He Claims Hormuz a ‘New U.S. Territory’

    IPOs for Open AI, Anthropic could unlock 0B for charity

    IPOs for Open AI, Anthropic could unlock $430B for charity

    Poll showing Bass leading Raman by double digits was bogus, company says

    Poll showing Bass leading Raman by double digits was bogus, company says

    Multiple people dead as flooding continues in Indiana : NPR

    Multiple people dead as flooding continues in Indiana : NPR

    Sophie Cunningham defends stance on biological men in women’s sports

    Sophie Cunningham defends stance on biological men in women’s sports

    Democrats Make South Carolina the First Contest in 2028 Presidential Primary Calendar

    Democrats Make South Carolina the First Contest in 2028 Presidential Primary Calendar

    Has the Left Really Met Its Limit? 

    Has the Left Really Met Its Limit? 

  • 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

Is Vietnam’s Energy Transition Entering a New Phase? – The Diplomat
Business Asia

Is Vietnam’s Energy Transition Entering a New Phase? – The Diplomat

August 19, 2026
Kyrgyzstan Liquidates More Companies Over Sanctions Risks – The Diplomat
Business Asia

Kyrgyzstan Liquidates More Companies Over Sanctions Risks – The Diplomat

August 18, 2026
Iran, Tajikistan Finalizing Fuel Export Agreement – The Diplomat
Business Asia

Iran, Tajikistan Finalizing Fuel Export Agreement – The Diplomat

August 18, 2026
Uzbekistan Launches Its First Tax-Free Crypto Mining Zone – The Diplomat
Business Asia

Uzbekistan Launches Its First Tax-Free Crypto Mining Zone – The Diplomat

August 17, 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
      • UK
      • USA
      • Weather
      • World
      No Result
      View All Result

      Recent News

      Austin Abrams fights to escape a 60-ton sperm whale in new ‘Whalefall’ trailer with Josh Brolin

      Austin Abrams fights to escape a 60-ton sperm whale in new ‘Whalefall’ trailer with Josh Brolin

      August 20, 2026
      Prince Harry and Meghan Markle reportedly returning to Britain – National

      Prince Harry and Meghan Markle reportedly returning to Britain – National

      August 20, 2026
      Holistic Health Hacks: Oils for Everyday Wellness

      Holistic Health Hacks: Oils for Everyday Wellness

      August 20, 2026
      Republicans Are Going To Get Destroyed Because Trump Won’t Talk About Affordability

      Republicans Are Going To Get Destroyed Because Trump Won’t Talk About Affordability

      August 20, 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