# Script to play a beep sound on Windows devices

The Windows ***beep*** command produces a beeping sound through the computer’s speaker. This command can be employed to test the speaker’s functionality or generate various signals for the user. Predominantly, generating such reverberations can signal the user about an operation that has been completed or that the device needs attention. With Hexnode UEM admins can remotely play a beep sound for a specified period on Windows devices by using customized [scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/).

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell script
-----------------

Executing this script will play the beep sound on the device.

PowerShell script to play beep sound

try{ #determining the current logged in user $currentLoggedInuser="" $signedInUsers=get-wmiobject win32\_process | where-object {$\_.processname -eq "explorer.exe"} | Foreach ({$\_.getowner().user}) foreach($user in $signedInUsers) { $isactive = C:\\windows\\system32\\qwinsta.exe $user if($isactive -like "\* Active\*") { $currentLoggedInuser = $user break } } if($currentLoggedInuser -eq "") { Write-Host "cannot find any logged in user, so skipping script execution`nTo execute this script, atleast one user must be logged-in to the device" } else { Write-Host "Device beep generated for user: ",$currentLoggedInuser $script = "Start-Process ""$file"" -PassThru -Wait" #unregistering ShowNotificationTask task from task scheduler (if we have already registered using script execution) try { $a=Unregister-ScheduledTask -TaskName "ShowNotificationTask" -Confirm:$false -ErrorAction:Ignore } catch { Write-Host "error while unregistering sheduletask-->",$\_.Exception.Message } #scheduling task $actions = (New-ScheduledTaskAction -Execute 'powershell.exe' -WorkingDirectory C:\\Hexnode\\Logs -Argument "-ExecutionPolicy unrestricted -WindowStyle Hidden -NoLogo -command `"&{\[console\]::Beep(1000,5000)}`"") $principal = New-ScheduledTaskPrincipal -UserId $currentLoggedInuser -RunLevel Highest $settings = New-ScheduledTaskSettingsSet -WakeToRun -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -Hidden -AllowStartIfOnBatteries -Priority 0 -StartWhenAvailable $task = New-ScheduledTask -Action $actions -Principal $principal -Settings $settings $reg= Register-ScheduledTask 'ShowNotificationTask' -InputObject $task Start-ScheduledTask -TaskName "ShowNotificationTask" -Verbose } } catch { Write-Host "Exception inside running script-->",$\_.Exception.Message }

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

  try{

 \#determining the current logged in user

 $currentLoggedInuser=""

 $signedInUsers=get-wmiobject win32\_process | where-object {$\_.processname -eq "explorer.exe"} | Foreach ({$\_.getowner().user})

 foreach($user in $signedInUsers)

 {

 $isactive = C:\\windows\\system32\\qwinsta.exe $user

 if($isactive -like "\* Active\*")

 {

 $currentLoggedInuser = $user

 break

 }

 }

 if($currentLoggedInuser -eq "")

 {

 Write-Host "cannot find any logged in user, so skipping script execution`nTo execute this script, atleast one user must be logged-in to the device"

 }

 else

 {

 Write-Host "Device beep generated for user: ",$currentLoggedInuser

 $script = "Start-Process ""$file"" -PassThru -Wait"

 \#unregistering ShowNotificationTask task from task scheduler (if we have already registered using script execution)

 try

 {

 $a=Unregister-ScheduledTask -TaskName "ShowNotificationTask" -Confirm:$false -ErrorAction:Ignore

 }

 catch

 {

 Write-Host "error while unregistering sheduletask-->",$\_.Exception.Message

 }

 \#scheduling task

 $actions = (New-ScheduledTaskAction -Execute 'powershell.exe' -WorkingDirectory C:\\Hexnode\\Logs -Argument "-ExecutionPolicy unrestricted -WindowStyle Hidden -NoLogo -command `"&{\[console\]::Beep(1000,5000)}`"")

 $principal = New-ScheduledTaskPrincipal -UserId $currentLoggedInuser -RunLevel Highest

 $settings = New-ScheduledTaskSettingsSet -WakeToRun -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -Hidden -AllowStartIfOnBatteries -Priority 0 -StartWhenAvailable

 $task = New-ScheduledTask -Action $actions -Principal $principal -Settings $settings

 $reg= Register-ScheduledTask 'ShowNotificationTask' -InputObject $task

 Start-ScheduledTask -TaskName "ShowNotificationTask" -Verbose

 }

}

catch

{

 Write-Host "Exception inside running script-->",$\_.Exception.Message

}

   

 

  

In the above script, there are two values mentioned next to the beep command. The first value specifies the frequency of the beep sound which ranges from 37 to 32767 and the second value is to specify the time period in milliseconds for how long the beep sound should prevail.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.