Powershell script for reading Excel Table and send e-mails

Mayckel TS 21 Reputation points
2024-04-09T17:30:10.8566667+00:00

Hello community,

Basically I would like to have a powershell code that reads a Table inside an Excel WorkBook and send e-mails to some recipients based on a specific column.

That table contains some user's information and the e-mail must be sent to the manager's address so he can evaluate their employees access permissions.

Source table:User's image

My pain is to read the table and send only one e-mail to each manager.

Examples:

E-mail to the first manager:

E-Mail to the second manager:

E-mail to the third manager:

Any help would be much appreciated.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,509 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,711 Reputation points
    2024-04-09T20:03:42.16+00:00

    To read the Excel spreadsheet, use the ImportExcel PowerShell module. Installing it should be as easy as Install-Module ImportExcel.

    Here's an example of grouping the users by their managers and sending just on mail to each manager:

    Import-Excel C:\junk\group.xlsx |				# this usage expects one worksheetm but you have multiple sheets, You just have to name the sheet.
        Group-Object -Property 'Manager E-mail' |
            ForEach-Object {
                $to = $_.Name       # manager email address
                $MyPeeps = @()
                $_.Group|
                    ForEach-Object {
                        $MyPeeps += $_ | Select-Object Name,E-Mail,Info
                }
                $emailbody = "Below are the following blah blah blah`n`n"
                # format the users in $mypeeps in the way you want them to appear
                # you can use HTML if you like.
    
                #
                # add whatever other properties you need to the Send-MailMessage
                Send-MailMessage -To $to -Body $emailbody ....      # send the message
            }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.