PowerShell/Docs/set-volume.md

77 lines
1.8 KiB
Markdown
Raw Normal View History

2022-11-18 17:02:20 +01:00
## The *set-volume.ps1* PowerShell Script
2022-11-17 19:46:02 +01:00
This PowerShell script sets the audio volume in percent.
## Parameters
```powershell
set-volume.ps1 [-Volume] <Int32> [<CommonParameters>]
-Volume <Int32>
Specifies the percent number
Required? true
Position? 1
Default value 0
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
## Example
```powershell
PS> ./set-volume 50
```
## Notes
Author: Markus Fleschutz | License: CC0
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Sets the volume
.DESCRIPTION
This PowerShell script sets the audio volume in percent.
.PARAMETER volume
Specifies the percent number
.EXAMPLE
PS> ./set-volume 50
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
Param([Parameter(Mandatory=$true)] [ValidateRange(0,100)] [Int] $Volume)
try {
# Create the Windows Shell object.
$obj = New-Object -ComObject WScript.Shell
# First, set volume to zero.
1..50 | ForEach-Object { $obj.SendKeys( [char] 174 ) }
# Calculate number of (volume up) key presses
$keyPresses = [Math]::Ceiling( $Volume / 2 )
# Raise volume to specified level.
for( $i = 0; $i -lt $keyPresses; $i++ ) {
$obj.SendKeys( [char] 175 )
}
2022-11-18 17:02:20 +01:00
& "$PSScriptRoot/speak-english.ps1" "$($Volume)% volume."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2022-11-17 19:46:02 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of set-volume.ps1*