Search code examples
batch-filecmdcommandmsg

Mass sending message via CMD


I'm trying to create a batch file which allow me to send message to mass users via cmd. Would appreciate any help as Im new to coding. Thank you.

Currently my code is like this.

@ECHO OFF

MSG /SERVER:hostname1 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send

MSG /SERVER:hostname2 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send

MSG /SERVER:hostname3 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send

pause
ECHO ON

I have a lot of hostnames thus I want to make my life easier. I'm trying to have a code something like this.

@ECHO OFF

SET hostname=hostname1, hostname2, hostname3
SET message=Hello User, Please update your laptop

MSG /SERVER:%hostname% /TIME:60 /v /w * %message%
echo message send

pause
ECHO ON

Solution

  • You'll need to create a loop to send the message to each hostname one by one.

    In this code, we use the FOR loop to iterate over each hostname in the hostnames variable.

    The MSG command is executed for each hostname


    @ECHO OFF
    SET hostnames="hostname1","hostname2","hostname with spaces"
    SET message=Hello User, Please update your laptop
    FOR %%i IN (%hostnames%) DO (
      MSG /SERVER:%%i /TIME:60 /v /w %message%
      echo message send to %%i
    )
    pause