Powershell Script to Export recently created AD Accounts of Particular Attributes in CSV Format to Sending Mail to Specific Mail account

Sathishkumar Singh 486 Reputation points
2023-01-10T13:19:12.45+00:00

Hello All

I want perfect PowerShell script When (Newly) with in 24 Hours created of AD Accounts Trigger with event id to Export particular Attributes in CSV Format -After that Sending Mail to Specific Mail account

277877-2023-01-10-18-35-02-users-01-10-2023-11-16-amcsv-e.png

Please advise

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,603 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rafael da Rocha 5,176 Reputation points
    2023-01-10T16:36:25.473+00:00

    Hello @Sathishkumar Singh , here's how to get the information. You'll then have to decide on the method to send the email, depending on what's available in your organization.

    $Last24h = (Get-Date).AddHours(-24)
    $Users = Get-ADUser -Filter { whencreated -ge $Last24h } -Properties mail, Name, GivenName, Surname, Office, telephoneNumber, manager, Department
    
    $output = foreach ($user in $users) {
        $manager = $null
        if ($null -ne $user.manager) {
            $manager = Get-ADUser $user.Manager -Properties Name, mail
        }
        [PSCustomObject] @{
            Email           = $user.mail
            Name            = $user.Name
            'First Name'    = $user.GivenName
            'Last Name'     = $user.Surname
            Location        = $user.Office
            'Phone Number'  = $user.telephoneNumber
            'Manager Name'  = $manager.Name
            'Manager Email' = $manager.mail
            Department      = $user.Department  
        }
    }
    
    $output | Export-Csv -Path 'C:\scripts\Data.csv'
    

    If this or any other reply helped solve your question, please remember to upvote and/or "Accept Answer". It helps others facing similar issues find the solution.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jordan Millama 1,366 Reputation points
    2023-01-10T16:39:26.033+00:00

    I suggest that Power Automate may be better suited for this. Head over to their community forum, FL_Comm_Forums, I'm sure someone will be able to help you out.


    Please accept as an answer if this was helpful.

    0 comments No comments

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.