PowerShell/scripts/set-volume.ps1

36 lines
874 B
PowerShell
Raw Normal View History

2024-10-01 15:24:16 +02:00
<#
2022-07-28 08:50:50 +02:00
.SYNOPSIS
2023-09-17 11:31:10 +02:00
Sets the audio volume
2022-07-28 08:50:50 +02:00
.DESCRIPTION
2023-09-17 11:31:10 +02:00
This PowerShell script sets the audio volume to the given value in percent (0..100).
.PARAMETER percent
Specifies the volume in percent (0..100)
2022-07-28 08:50:50 +02:00
.EXAMPLE
2023-09-17 11:31:10 +02:00
PS> ./set-volume.ps1 50
2022-07-28 08:50:50 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-09-17 11:31:10 +02:00
Param([Parameter(Mandatory=$true)] [ValidateRange(0,100)] [Int] $percent)
2022-07-28 08:50:50 +02:00
try {
# Create the Windows Shell object.
$obj = New-Object -ComObject WScript.Shell
# First, set volume to zero.
2023-09-17 11:31:10 +02:00
for ([int]$i = 0; $i -lt 100; $i += 2) {
$obj.SendKeys([char]174) # each tick is -2%
}
2022-07-28 08:50:50 +02:00
# Raise volume to specified level.
2023-09-17 11:31:10 +02:00
for ([int]$i = 0; $i -lt $percent; $i += 2) {
$obj.SendKeys([char]175) # each tick is +2%
2022-07-28 08:50:50 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}