PowerShell Script To Send Message To Network Computer(s)

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

I have created a this script which can send a POP UP MESSAGE to the computer(s). When you run the script it will simply prompt for inputs, 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 containing computers’ name, one in each line.

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 should have one computer name in each line.

Time :- Time :- Type time in SECONDS, duration 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 remain till 5 seconds on user’s desktop.

You can Download the script from here, or copy the below code and save as “.PS1” file.

<# Script to send message to Client computers using MSG utility.
The script will prompt you to enter the following information.
Message :-  Type your message which you want to send to the computers. Like "Your computer is pending reboot after Windows updates, please reboot.."
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 “,” like PC1, PC2, PC3 or you can give the path of the txt file with computer names, one in each line.
Like below.
PC1
PC2
PC3

Time :- Type time in SECONDS, duration till message pop up will remain on computer. After this time, the message pop up will disappear.
For example if you provide 5 in time prompt, the pop up will 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

35 comments

  • 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

  • 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.

  • thank the problem is solved I did what you suggested

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

  • and im confused how will i use this script??

  • 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?

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

    • 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]

  • 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!

  • atrent@geosolinc.com

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

  • 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

  • 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

    • 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.

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

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

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

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

    • 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.

      • HI Subodh,

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

  • You need to allow below reg key.

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

  • Is there anyway to add visuals to message

  • How can I modify your script to pull computer name from a text file?

  • Thank you! I didn’t notice you can put the file path of a text file with computer names…this is awesome!

  • I guess SCCM can do this. Just research there are tools that uses a client software installed on your cluents The Script Worked perfectly in my Test Environment, but in the Production Environment it takes too Long to get executed. is there anything to help to quicken it?

    • Yes, it will take little long if you are sending to large amount of computers. But, with new Powershell versions you can reduce the time. Try to use foreach -parallel or just divide the input files and create multiple sessions to send.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s