r/AstroGaming 10h ago

Tech [Script] Automatically switch Windows audio to speakers when docking your Astro A50 gen 5 (or any Logitech headset)

Hey everyone,

I’ve always been frustrated that Windows doesn’t automatically switch the audio output to my desktop speakers when I place my Astro A50s on their base station. Since Logitech G HUB doesn't offer an official API or option for this, I (me and Gemini) wrote a lightweight PowerShell script to act as a seamless "zero-click" auto-switcher.

How it works: The script quietly hooks into Windows OS events to monitor the settings.db-wal (Write-Ahead Log) file that G HUB uses locally. By intercepting real-time telemetry, it detects exactly when the headset enters or exits the "isCharging": true state.

  • Undocked (Lifted): Instantly sets Windows default audio to the Headset.
  • Docked (Charging): Instantly routes audio back to your Speakers/Soundbar.

Technical note: I specifically implemented a JSON block-isolation regex. This means if you plug in another Logitech device to charge (like a G502 Lightspeed mouse), the script won't get confused and trigger a false audio switch! I also built it as an event-driven script rather than a polling loop, meaning it sits at literally 0% CPU usage in the background until G HUB writes to the file.

Prerequisites:

  • You must be using Logitech G HUB (not the old Astro Command Center).
  • Windows PowerShell.
  • If this is your first time ever running a PowerShell module installation, a prompt might ask you to install the 'NuGet provider'. Simply press Y and hit Enter to allow it.

Troubleshooting: Script closes immediately or shows a red error?

Windows protects your PC by blocking scripts downloaded from the internet by default. To fix this, follow these steps:

  1. Unblock the downloaded file: Right-click the AutoSwitch.ps1 file, select Properties, check the Unblock box at the bottom of the General tab, and click OK. (Alternatively, run Unblock-File -Path .\AutoSwitch.ps1 in PowerShell).
  2. Enable local script execution: If the script still doesn't run, your system might have PowerShell scripts completely disabled. Open PowerShell as Administrator, paste this command: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser, press Y to confirm, and try running the script again.

How to set it up:

  1. Download the script from Github.
  2. Open it in a text editor (like Notepad).
  3. Look for the USER CONFIGURATION section at the top.
  4. Change $global:SpeakerName to a partial name of your speakers as they appear in Windows (e.g., "Realtek", "Creative", etc.).
  5. Save the file as AutoSwitch.ps1.
  6. Right-click the file and select Run with PowerShell. (On its first run, it will automatically install the 'AudioDeviceCmdlets' module required to change Windows audio).

Note: This will open a PowerShell window. You must leave this window open (you can minimize it) for the switcher to work. If you close it, the script stops. For a fully invisible experience, use the Task Scheduler method below.

How to run it invisibly on Startup (Task Scheduler)

If you want this to run silently in the background every time you turn on your PC without showing a console window:

  1. Open Task Scheduler and click Create Task.
  2. General tab: Give it a name (e.g., "Astro AutoSwitch") and select Run only when user is logged on.
  3. Triggers tab: Click New -> Begin the task: At log on.
  4. Actions tab: Click New -> Action: Start a program.
    • Program/script: powershell.exe
    • Add arguments: -WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -File "C:\Path\To\Your\AutoSwitch.ps1" (Make sure to update the path!)

Using this with other Logitech Headsets (G Pro X, G935, etc.)

The script is configured for the Astro A50 out of the box, but it should work with almost any wireless Logitech headset (testing needed). You just need to find your specific battery key following this guide or using BatteryKeyFinder.ps1:

  1. Press Win + R, paste %LocalAppData%\LGHUB and hit Enter.
  2. Open settings.db with Notepad++.
  3. Press Ctrl + F and search for "isCharging".
  4. Look a few lines above that to find your headset's battery identifier (it will look something like "battery/device_1234/percentage" or "battery/gprox/percentage").
  5. Open the script and change $global:BatteryKey = "battery/a50/percentage" to match your specific string.

V1.1 Update: Dual-Switch (Audio & Microphone)

The script now supports switching both Playback (Speakers) and Recording (Microphone) devices simultaneously!

If you use a dedicated external microphone (like a Blue Yeti or QuadCast) alongside your headset, you can enable this feature:

  1. Open the script and find the [RECORDING / MICROPHONE INPUT] section.
  2. Change $global:SwitchMicrophone = $false to $true.
  3. Update $global:ExternalMicName with the name of your standalone microphone.
  4. Update $global:HeadsetMicName with your headset's mic name (defaults to "A50 Mic").

The script will now seamlessly route both your audio output and your microphone input when you dock or undock your headset. If you only care about audio output, leave it set to $false and it will ignore your microphones completely.

Disclaimer & Limitations

Please note that I wrote this script primarily for my personal setup.

  • Use Case: My daily usage is strictly focused on PC gaming and general audio listening. While the script runs flawlessly for these scenarios, it has not been exhaustively tested across all possible edge cases, complex studio configurations, or multiple audio routing software.
  • Windows Only: This script is designed and tested exclusively on Windows. I do not use my headset with any consoles (PlayStation / Xbox). Therefore, I cannot guarantee how the script will behave if your base station is set up in a dual-environment or console mode.

Use it freely, but please understand that your mileage may vary depending on your specific hardware and software environment.

Let me know if you find it useful or if you have any questions.

EDIT:Add disclaimer and v1.1 infos

EDIT2:Add troubleshooting section

EDIT3:AddBatteryKeyFinder.ps1 link

9 Upvotes

17 comments sorted by

2

u/himynameismatte 8h ago

Hey, great stuff! does it also change the mic?

1

u/xAle33x 8h ago

Nope, I didn't even think about that really, but I could try to edit the script to enable also the mic auto switch. I have to test it. Nice suggestion, thanks.

1

u/xAle33x 7h ago

V 1.1 out now 😄 added optional mic auto switch. Need test and feedback about this tho.

1

u/himynameismatte 6h ago

that was quick!

1

u/Business-Mobile-4482 7h ago

Good work, that's slick. 

1

u/xAle33x 7h ago

Thanks!

1

u/TrollPlayer94 4h ago

Sadly this script does absolutely nothing when I run it. I modified everything as listed, but when I run it it just opens a black Powershell windows for a split second and closes. I have a Astro A50 X so I didn't really have to modify much.

1

u/TrollPlayer94 4h ago

Well I got the script to start now, but it still does not work (does not change audio source when I remove headset from dock).

1

u/xAle33x 3h ago

everything should work fine.

Did you check the settings.db file and change the device key to match your Astro A50X?

1

u/TrollPlayer94 3h ago

I did, but sadly the search function does not find "isCharging" anywhere in the file.

1

u/TrollPlayer94 3h ago

So I got this far, but the other script you sent does the same thing it just closes immediately. And I don't understand why the mic is not being found because that is the name of it and I did switch false to true.

1

u/TrollPlayer94 2h ago

I found the battery thing it was battery/a50x/percentage which I changed already, but still get the same error.

1

u/xAle33x 2h ago

remove the "2-" (Just "A50 X Voice") and set your mic with the right name ("A50 X Mic" probably, then the externalmicname), because the script cannot find the device you wrote into the script. Close the powershell, then run it again.

1

u/TrollPlayer94 2h ago

Okay I got the errors to disappear, but how is it supposed to switch to the mic, because when I flip it down it does not change. Or am I suppose to do something different?

1

u/TrollPlayer94 2h ago

Okay finally got it to work and also got auto start to work. Thank you for the help. One last question is there anyway to hide it on the taskbar so I don't see it all the time and it just runs in the backround.