Is there a way to beep in the console using POWERSHELL
Asked Answered
A

3

6

I'm just starting to learn how to code in Powershell. So, how can I do dis?

I searched the MS website but it is not clear...

Atiptoe answered 27/10, 2022 at 16:8 Comment(2)
I mean, like, is it [console]::beep() or just beep() ???Atiptoe
You can also change the frequency and duration with params like this [console]::beep(500,300). The first one is frequency in hertz(ranging from 37 to 32767 hertz) and the second is duration in milliseconds.Amylopsin
G
15

PowerShell has no dedicated command for emitting a beep.

Therefore, use [Console]::Beep(), which you note as an option. This relies on the fact that you have virtually unlimited access to .NET APIs from PowerShell:

[Console]::Beep()

An alternative, available in Windows PowerShell v5.1 (the latest and last version - not sure about earlier ones) and in all versions of PowerShell (Core) 7, is to use escape sequence `a, representing the so-called BEL (BELL) character, inside an expandable string ("...").

Write-Host -NoNewLine "`a"

Note:

  • While "`a" alone would work too, it would also print a newline (which Write-Host -NoNewLine avoids).

  • Printing the BEL character to the console (terminal) rather than calling [Console]::Beep() (which uses the WinAPI) has one advantage: in terminal applications that support it, such as Windows Terminal, a visual indicator that the "bell was rung", so to speak, is shown in the window / tab title, which is helpful in situations where the sound is muted and/or you've been away from the terminal.

Geomancer answered 27/10, 2022 at 16:11 Comment(2)
I find it interesting that with windows-terminal, the former ([console]::beep()) will in fact play a sound, but won't trigger any of the terminal settings for "Bell notification style", however the latter (Write-Host "`a") will trigger bell notifications? Now I'm wondering if that's intentional, or some sort of side-effect of how console beep works...Sexed
@GarThor, good question: please see my update (last bullet point).Geomancer
B
4

To add to the @mklement0 answer, there is an old Script Doctor post which highlights how you can change the tone/pitch and duration of the beep, as well.

To summarize (in case the link is removed):

[console]::beep() ## The default beep

[console]::beep(500, 300) ## param 1 is the tone; param 2 is the duration in ms; anything lower than 190, or higher than 8500 cannot be heard.

[console]::beep(2000, 500) ## a higher pitch, and longer duration

Boson answered 19/4, 2024 at 19:20 Comment(1)
I would like to add that "anything lower than 190, or higher than 8500 cannot be heard" isn't necessarily true. This number is the frequency in Hertz, and the very young human ear is capable of hearing up to around 20000 Hertz, which goes down by around 2000 Hertz for every 10 years you age. So 8500 Hertz would equal around 52.5 years.Aimless
P
2

These will play the default system sounds.

[System.Media.SystemSounds]::Asterisk.Play()
[System.Media.SystemSounds]::Beep.Play()
[System.Media.SystemSounds]::Exclamation.Play()
[System.Media.SystemSounds]::Hand.Play()
[System.Media.SystemSounds]::Question.Play()

Playing the rest of the Sound control panel events is much more complicated and not as easily accessed as these five defaults. It's "possible" but I've not yet come across anything that packages it up nicely.

Pothunter answered 5/7, 2024 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.