Send an Email with PowerShell

Applies to PowerShell
Modules needed: None

This is a fun little script I set up mostly to see if I could do it. It actually turned out to come in handy when I’m configuring email addresses and new mailboxes in exchange, and want to send a quick test email without changing windows.

The script asks for a recipient, a subject, and a body. It then confirms you want to send the email.

Note: The body is stored in HTML. This means if you want a line break, you need to type <br>in your line, as opposed to hitting ‘Enter’. 

You will want to change the “From”, “SmtpServer”, and “$Sig” lines to meet your needs. For the $sig variable, an HTML file containing your signature is expected.


Clear-Host
Write-Host "Send an Email" -ForegroundColor Yellow
Write-Host
Write-Host
$sig = Get-Content 
$to = Read-Host "To"
$subject = Read-Host "Subject"
$message = Read-Host "Message"
$body = $message + $sig
Write-Host
Write-Host
Write-Host "Message completed. Would you like to send?"
Write-Host
Write-Host "1) Yes"
Write-Host "2) No"
Write-Host
$selection = Read-Host "Select"
switch ($selection) {
'1' { Send-MailMessage -To $to -From  -Subject $subject -SmtpServer  -Body $body -BodyAsHtml
Write-Host "Message Sent." -ForegroundColor Green }
'2' { exit }
}

 

Add User to Distribution Group

Applies to Exchange, and O365.
Modules needed: Exchange, or Microsoft.Exchange.Management.ExoPowershellModule

This is a simple script that just asks for input on adding members to a distribution group. The reason I made this script was because I wanted to simplify this process for non-admin IT folks, and enter multiple users at a time.

To add multiple users, separate them with commas.
Example: Members: druggeri,tdanza,pporter


$dl = Read-Host "Distribution Group"
Write-Host
get-distributiongroup -identity $dl
Write-Host
Write-Host
$members = Read-Host "Members"
$members = $members.split(',')
foreach ($m in $members) {
  Add-DistributionGroupMember -identity $dl -member $m }
Write-Host
Write-Host "Confirmation:" -ForegroundColor Green
get-distributiongroupmember -identity $dl | Format-Table -AutoSize

At the end of the script, a confirmation is shown using get-distributiongroupmember on the entered group. This is just so you can verify your change applied, and that the correct users were added.