Home > PS Scripts > PowerShell Script To Send Message To Network Computer(s)

PowerShell Script To Send Message To Network Computer(s)

Some time back, we had a requirement of sending a text message to all the client computers to inform about some activity.

I have created a little script which can send a POP UP MESSAGE to the computer(s), and it will simply prompt for input, and at the time of finish it will show you all the statistics.

The script can accept a single computer name or multiple computer names separated with comma (,) or path of txt file which is containing the list of computers.

When you run this script it will ask you for below Input.

Message :- Type your message which you want to send to the computers. Like Hi, “Hello, Update are being installed on your computer, pleas wait” Etc.

Computer Name: – Type the computer name to which you want to send the message, if you type PC1, it will send message to PC1 computer.

You can type multiple computer names separated by the comma “,”. PC1, PC2, PC3 or you can give the path of the txt file which is containing the computer names in below manner

PC1

PC2

PC3

Time :- Type time in SECONDS, till message pop up will remain on computer. After this time, the message pop up will disappear.

Like if you provide 5 in time prompt, the pop up will be remain till 5 seconds on user’s desktop.

===========================================================

You are free to Copy, Modify and use this script,

I welcome your suggestions and views to improve this script code.

============================================================

Copy the below code and save this as “.PS1” extension

———————————————————————————————————————————-   

<# Script to send message to Client computers using MSG utility

It will prompt you to enter the following information.

Message :-  type your message which you want to send to the computers. Like Hi, Hello etc.

Computer Name :- Type the computer name to which you want to send the Message, if you type PC1, it will send message to PC1 computer.

You can type multiple computer names separated by the comma “,”. PC1, PC2, PC3 or you can give the path of the txt file which is containing the computer names in below manner

PC1

PC2

PC3

Time :- Type time in SECONDS,till message pop up will remain on computer. After this time, the message pop up will disappear.

Like if you provide 5 in time prompt, the pop up will be remain till 5 seconds on user’s desktop.

#>

# Variable declaration

$Start_Time       =              Get-Date -Format T

$logFile          =                    ‘Not_Reachable_PCs.txt’

$Message          =               Read-Host -Prompt “Type Your Message Here”      

$ComputerName     =       Read-Host -Prompt “Type Computer Name Here”    

$Time             =       Read-Host -Prompt “Type Time Here” 

$Session          =       “*”

$ComputerName     =       $ComputerName -split ‘,’

if ($ComputerName -match “:”)

                      {

                      $Path = $ComputerName

                      $ComputerName = Get-Content $path

          }

                      $Total = $ComputerName.count 

                                foreach ($Computer in $ComputerName )

                                                {

                                                                if (Test-Connection -ComputerName $Computer -Count 1 -ErrorAction 0)

                                {

                                                                Write-Host “Sending Message to $Computer…….” -ForegroundColor yellow

                                msg $Session /Server:$Computer /Time:$Time $Message

                                                                Write-Host “Message Successfully Sent to $Computer” -ForegroundColor Green

                                                                }

                                                                else

                                                                                {

                                                                Out-File -FilePath $logFile -InputObject $Computer -Append -Force

                                                                                                Write-Host “$Computer is not Reachable…” -ForegroundColor red

                                                                                }

                                                }

                                $Not_Reachable_Count  = @(Get-Content $logFile).count

        $End_Time   =    Get-Date -Format T

        $Minute = (New-TimeSpan -Start $Start_Time -End $End_Time).Minutes

        $Second = (New-TimeSpan -Start $Start_Time -End $End_Time).Seconds

                                Write-Host Start at $Start_Time, End At $End_Time, Took About $Minute Minutes $seconds Seconds

                                Write-Host “Total $Total Computer Processed, $Not_Reachable_Count computers were offline. The list is stored in $logFile” -ForegroundColor white

—————————————————–

Advertisements
Categories: PS Scripts
  1. Fady
    July 2, 2013 at 3:13 AM

    hi and thank you for this wonderful script but I have a problem that it gives me msg.exe : Error opening Terminal server pc-14
    At line:37 char:36
    + msg <<<< $Session /Server:$Computer /Time:$Time $Message
    + CategoryInfo : NotSpecified: (Error opening Terminal server pc-14:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    Error [1722]:The RPC server is unavailable.
    I disabled the firewall on all the computers and still gives me the same error any idea

    Best

  2. July 2, 2013 at 4:57 PM

    Please check the RPC Service on the remote and local computer and check the below reg key.

    the following registry key must be activated
    HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server
    Name : AllowRemoteRPC
    Type : REG_DWORD
    Value : 1

    Please let me know if see any issue.

  3. Fady
    July 4, 2013 at 1:12 PM

    thank the problem is solved I did what you suggested

  4. sahil
    August 10, 2013 at 4:48 PM

    hi where i have to use this script?? Command prompt or other???

    • September 8, 2013 at 10:17 PM

      This is Power Shell script, you need to use Power Shell for this
      Although, you can use command prompt but, ultimately you need to call Power Shell using a batch file.

  5. sahil
    August 10, 2013 at 5:02 PM

    and im confused how will i use this script??

    • September 8, 2013 at 10:18 PM

      Run this script with PowerShell, it will simply prompt you for the required input

  6. Mark Dininio
    September 5, 2013 at 1:30 PM

    Hi there. Thanks for the great work. I am testing the script and trying to send a message to two PCs on my network (one ismy own workstation). After going through the prompts it will send me the message but not to the other PCs although it says ‘Message successfully sent to…’. I have the other PC right beside me and I never see the message pop up? Thoughts?

    • Mark Dininio
      September 5, 2013 at 1:40 PM

      Update! The above reply about turning on the AllowRemoteRPC key in the registry fixed it. Not too secure though. Thanks.

    • September 8, 2013 at 10:23 PM

      Thanks !

      Can you please give me some brief that how are you using this ?

      Also, can you use below command to send message to your computer for which you are using this script ?

      msg * /Server:ComputerName /time:500 [Replace computer name with you orignal computer name]

  7. January 1, 2014 at 10:07 PM

    Hello! Thiss is my first coment here so I just wanted to give a quick shout out and saay I genuinely enjoy reading your blkog posts.
    Can you suggest any other blogs/websites/forums that go over the same subjects?
    Appreciate it!

  8. atrent@geosolinc.com
    January 8, 2014 at 11:45 AM

    I changed “AllowRemoteRPC” registry entry like stated above. However, I am still getting, “msg : Error 5 getting session names”

  9. Adedamola
    March 17, 2014 at 10:13 AM

    Hi, nice write up. I could not run the ps. Getting the term is not recognized as the name of the commandlet. Any clues? Can u send the file as an attachment to me on thamola@yahoo.com

  10. April 16, 2014 at 2:12 AM

    use lansend to send messages to multiple computers http://lizardsystems.com/lansend/

  11. natewhit44
    August 28, 2014 at 4:04 PM

    Hello,

    I am getting the following error, would someone please help me diagnose the issue?

    msg.exe : Error 5 getting session names
    At C:\Users\NWhitlock\Downloads\MessageOut.ps1:61 char:36
    + msg <<<< $Session /Server:$Computer /Time:$Time $Message
    + CategoryInfo : NotSpecified: (Error 5 getting session names:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    • August 28, 2014 at 4:22 PM

      Please check the RPC Service on the remote and local computer and check the below reg key.
      the following registry key must be activated
      HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server
      Name : AllowRemoteRPC
      Type : REG_DWORD
      Value : 1
      Please let me know if see any issue.

      • natewhit44
        August 28, 2014 at 4:38 PM

        This reg. setting would need to be adjusted on each device over the network if this was to work, correct?

      • November 8, 2016 at 5:24 AM

        Correct, you can use GPO to do it.

  12. stroyindustriya
    September 30, 2014 at 6:28 AM

    Для тех, кому нужен проект канализации в Сочи?
    Наша организация «Строй-Индустрия» реализует услуги в области проектирования водоснабжения.
    Мы – это высокая организаторская способность. Мы – это инжиниринг высокого уровня. С нами вы экономите минимум 20 т.р.,в Водоканале проект стоит от 50 т.р.
    89881858121
    От Вас:

    – топосъемка M1:500
    – паспорт
    – технические условия
    Срок 5 дней. Результат – проект под ключ! Проект канализации в подарок.

  13. Guru Prasad
    October 17, 2016 at 12:54 AM

    Hello,
    Thank you for script
    How can we send message to domain computers or computers in OU.

    • November 8, 2016 at 5:28 AM

      You can export the list of computers from OU and then use it.

  14. Guru Prasad
    October 18, 2016 at 1:30 AM

    Hello how to reduce time in case if some of systems in between is not in network

    • November 8, 2016 at 5:27 AM

      This script will not broadcast the message, but will send message to all computers one by obe, you can split you list of computers to multiple list and then send it from different PS console.

      • Guru Prasad
        November 8, 2016 at 5:34 AM

        HI Subodh,

        can you please provide script to modify AllowRemoteRPC registry key to 1 on remote computers.

  15. November 8, 2016 at 8:03 AM

    You need to allow below reg key.

    HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server
    Name : AllowRemoteRPC
    Type : REG_DWORD
    Value : 1

    • Guru Prasad
      November 8, 2016 at 9:35 PM

      Do you have script to enable Registry key on remote computers

      • November 8, 2016 at 9:45 PM

        No, you can configure GPO to do that.

  16. Guru Prasad
    December 6, 2016 at 5:10 AM

    Is there anyway to add visuals to message

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Noam's scripting blog

"Ready to use" scripts and scripting tips for system admins with detailed walkthroughs/explanations.

%d bloggers like this: