Home Blog Archive Software Email using PowerShell send

Email using PowerShell send

  • Apr 05, 2026
  • 742
  • 0

With Windows Powershell, you can send directly from the System E-Mails. This basically is relatively easy, but fails in practice, often because you need to authenticate to the mail provider yet. We will show you how nevertheless.

E-Mails with the PowerShell send

For security reasons, the PowerShell does not allow passwords for the mail server in clear text in the script. Therefore, you need to store this data in encrypted form, externally, and with a Trick mount.
  1. For mail delivery, you use the Commandlet "Send-Mailmessage ", which supports all the usual parameters. For more detailed information, please see "Get-Help Send-Mailmessage ".
  2. At least you need the sender, recipient, subject, and content, as well as the outgoing mail server.
  3. If the Server is not passed explicitly uses the PowerShell, the contents of the variable "$PSEmail Server". So you can place this Server for repeated sending of mails already in the script or, even better, already in its own PowerShell profile.
  4. To do this, open about by means of "touch pad.exe $profile" in the profile file and add there, for example, the command "$PS-email server= "smtpmail.t-online.de"". As value use the relevant Server for your mail account.
  5. Many mail servers require a login with user name and password. At least the Former, you can pass the Parameter "credential" when you call. The script then when run in a login dialog box, in which the lack of a password is to be entered. But this is impractical and for script tasks in the Background is unsuitable.
  6. Since the password is not directly in the script, you must generate the required PSCredential object by reading the password from an encrypted file. To Create this file, use the command "(Get-Credential).password | ConvertFrom - SecureString > kennwort.txt".
  7. In the following, typical login dialog enter the "user name" and "password" and confirm with "OK". The PowerShell encrypts the password and stores it in the target file specified "password. txt".
  8. Later in the script, you retrieve the password again and save it as a Variable with "$password=Get-Content kennwort.txt | ConvertTo-SecureString".
  9. Now you can generate the necessary Credential object with the command "$credential=New-Object System.Management.Automation.PSCredential "username", $password". This object is finally passed via the "Send-Mailmessa ge" in addition, with the Parameter "- credential $credential".

YOU MAY ALSO LIKE

0 COMMENTS

LEAVE A COMMENT

Human?
1 + 3 =