PoshCode Logo PowerShell Code Repository

Send-SMSMessage by Jan Egil Ring 6 years ago
View followups from patrik | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1999"></script>download | new post

Function to send text messages (SMS) from Windows PowerShell using the Microsoft Outlook SMS Add-in.

For more information, see the following blog-post:
http://blog.powershell.no/2010/07/21/send-text-messages-sms-using-windows-powershell

  1. #requires -version 2
  2.  
  3. function Send-SMSMessage {
  4.         <#
  5. .SYNOPSIS
  6. Send a Text Message (SMS) using Microsoft Outlook
  7. .DESCRIPTION
  8. Sends a Text Message (SMS) using the supplied parameters.
  9. .PARAMETER To
  10. Telephone number to send the text message to.
  11. .PARAMETER Message
  12. The message to send.
  13. .EXAMPLE
  14. Send-SMSMessage -To 555-12345 -Message "This is a test message"
  15. .NOTES
  16. Requires Windows PowerShell v2 and Microsoft Office Outlook 2010.
  17. Not tested in Outlook 2003 or 2007, but it should work with the Microsoft Outlook SMS Add-in installed (download from Microsoft).
  18. AUTHOR:    Jan Egil Ring
  19. BLOG:      http://blog.powershell.no
  20. LASTEDIT:  21.07.2010
  21. #>
  22.  
  23.         [CmdletBinding()]
  24.         param (
  25.         [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  26.         $To,
  27.         [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  28.         $Message
  29.         )
  30.  
  31.         #Check if Outlook are running
  32.         $OutlookState = Get-Process | Where-Object {$_.Name -eq "outlook"}
  33.  
  34.         #Check whether Outlook is installed
  35.         if (-not (Test-Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE")) {
  36.                 Write-Warning "Outlook are not installed. The message was not sent."
  37.                 break
  38.         }
  39.  
  40.         #Check wheter an Text Messaging (SMS) account are set up in Outlook
  41.         $outlook = New-Object -ComObject outlook.application
  42.         if (-not (($outlook.Session.Accounts | Where-Object {$_.AccountType -eq "5"} | Measure-Object).Count -gt 0)) {
  43.                 Write-Warning "Outlook are installed, but no accounts are configured for Text Messaging (SMS). The message was not sent."
  44.                 break
  45.         }
  46.  
  47.  
  48.         #Send message
  49.         $NewMessage = $outlook.CreateItem("olMobileItemSMS")
  50.         $NewMessage.To = $To
  51.         $NewMessage.Body = $Message
  52.         $NewMessage.Send($true)
  53.  
  54.         #Close outlook.exe if it was not running before executing this function  
  55.         if (-not $OutlookState) {
  56.                 $outlook.Quit()
  57.         }
  58.  
  59.  
  60. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me