Skip to content

Set-Volume

Sets the audio volume

Param([Parameter(Mandatory=$true)] [ValidateRange(0,100)] [Int] $percent)

try {
	# Create the Windows Shell object. 
	$obj = New-Object -ComObject WScript.Shell
    
	# First, set volume to zero. 
	for ([int]$i = 0; $i -lt 100; $i += 2) {
		$obj.SendKeys([char]174) # each tick is -2%
	}
    
	# Raise volume to specified level. 
	for ([int]$i = 0; $i -lt $percent; $i += 2) {
		$obj.SendKeys([char]175) # each tick is +2%
	}
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the volume in percent (0..100)

An interactive directory of PowerShell scripts.