I did this a while back using a utility. You muse use HomeBrew to install switchaudio-osx.
Someone create a webpage giving instructions how to install and use switch audio-ox. I don't want to take credit for their work. Here is the web page:
Here is a summary and usage of the app:
To Install:
brew install switchaudio-osx
Usage:
SwitchAudioSource -s "name of audio device"
example: SwitchAudioSource -s "MacBook Pro Speakers"
Here is a list of command switches:
SwitchAudioSource [-a] [-c] [-f format] [-t type] [-n] -s device_name | -i device_id | -u device_uid
-a : shows all devices
-c : shows current device
-f format : output format (cli/human/json). Defaults to human.
-t type : device type (input/output/system). Defaults to output.
-n cycles the audio device to the next one
-i device_id : sets the audio device to the given device by id
-u device_uid : sets the audio device to the given device by uid or a substring of the uid
-s device_name : sets the audio device to the given device by name
Source: Switch audio output via command line on MacOS - By Pete Houston