param( [Parameter(Mandatory=$true)] [ValidateSet("play_pause","next","prev","stop")] [string]$Action ) Add-Type -Name MediaKey -Namespace Win32 -MemberDefinition @' [DllImport("user32.dll")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, System.UIntPtr dwExtraInfo); '@ $vk = switch ($Action) { "play_pause" { 0xB3 } "next" { 0xB0 } "prev" { 0xB1 } "stop" { 0xB2 } } [Win32.MediaKey]::keybd_event($vk, 0, 0, [UIntPtr]::Zero) [Win32.MediaKey]::keybd_event($vk, 0, 2, [UIntPtr]::Zero)