1

With my little knowledge of writing code, I am trying to make a batch file where I can disable or enable a line in audio device with an IF statement.

For example, if the device is currently enabled, when I run the .bat file it will disable. If it's disabled, then it enables it.

I have the the device ID ("SWD\MMDEVAPI{0.0.1.00000000}.{55e90a30-8001-4bc8-af56-52998c03ed88}"). I am currently using pnputil /disable-device which works. But I don't know how to go about the IF statement. Would appreciate some help with that.

Edit: Okay so I found a tool called SoundVolumeView.exe that can lower the sound so the white noise can't be heard on startup. It can also mute as well all through command line.

Ameer
  • 21
  • 3

1 Answers1

2

I wasn't sure if you had solved this yourself already and I know I'm not supposed to spoon feed code on here but I had a script that did something similar and thought I should contribute. If you have any issues with this script, please comment with your issue.

Explaination: it runs pnputil /enum-devices /ids and looks at the output until it gets to a line with the device id. It sets a variable called ahead to 4. If ahead is above 0, then it will check if it is 0, if it isn't then it will go to the next line in the output of the command. If it is 0, then it will go to the toggle label and then it will check the line if it doesn't say Started. If it doesn't have Started in the line, then it will enable your device, if it does say started, then it will disable it. Then it just pauses and exits.

@echo off
setlocal enableDelayedExpansion
REM Needs to be run as administrator to work

set device=SWD\MMDEVAPI{0.0.1.00000000}.{55e90a30-8001-4bc8-af56-52998c03ed88}

set ahead=-1

for /f "delims=" %%a in ('pnputil /enum-devices /ids') do (
  set "line=%%a"
  if !ahead! GEQ 0 (
    if !ahead!==0 goto :toggle
    set /a ahead-=1
  ) else (
    if not "x!line:%device%=!"=="x!line!" (
      set ahead=4
    )
  )
)

:toggle
if "x!line:Started=!"=="x!line!" (
  REM Device is not running
  pnputil /enable-device "%device%" >nul
  echo Enabling device...
) else (
  REM Device is running
  pnputil /disable-device "%device%" >nul
  echo Disabling device...
)
pause
exit /b 0

For documentation on PnPUtil: https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/pnputil-command-syntax

For information on on for loops: https://ss64.com/nt/for.html, https://stackoverflow.com/a/50704943/19341457

For information on finding if a substring is in a string: https://stackoverflow.com/a/7006016/19341457

For information on delayed expansion: https://ss64.com/nt/delayedexpansion.html

exro
  • 36
  • 5
  • I had made an persistent variable in the end, and used values being assigned after enabling or disabling so it would do the opposite action the next time which worked. However, disabling that device, whilst showing in device manager that it was disabled, didn't actually stop the sound output. In the end SoundViewManager + a .bat script to automate that worked in the end. Thanks for your answer though, it was what I was looking for originally. – Ameer Jun 16 '22 at 10:44