Is it possible to email Powershell output to certain email address?

Shamyog 1 Reputation point
2022-04-12T16:08:19.527+00:00

Hello,

I am trying to email the following PowerShell output to a certain email address. Is there a way to do that?

Search-ADAccount -AccountExpiring -TimeSpan "365" | Select-Object Name, SamAccountName, AccountExpirationDate | Sort-Object AccountExpirationDate

Thank you.

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

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 95,021 Reputation points MVP
    2022-04-12T17:22:48.173+00:00

    Hi @Shamyog ,

    one option could be using Send-MailMessage: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.2

    Just put the query result of your command in a variable and it's possible to use this as body of the email.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments