I have already written scripts like this in the past but this is also my knowledgebase and I am in the habit of writing everything so there's a record. But there is something special about this script writing attempt. I didn't have any persons help. I did use ChatGPT to troubleshoot and check work but I had ChatGPT write a script to see what it would look like but I had also looked up online something about writing a script to change the audio output device since that is a thing I have to do all the time. I wasn't going to do this because a few weeks ago I thought when I decided then to do this, well, it only saves a few clicks. I mean, I don't remember exactly what my rationale was but I decided not to do it and then in dealing with it today where once again I had to change audio devices a couple of times and hated leaving the audio settings window open when I knew I was just going to have to change it later. And suddenly I had the idea, what if I wrote a script but the script simply changes the audio output device to whatever the next option is. If there is more than one option, I want it to cycle through all of them and if it reaches the end of the list, I want it to start over. These are all questions I had, whether it would do this automatically or if the script would have to be further complicated in order to do this. I have other scripts that do the same thing, and just switch the option to whatever the other option is using the simple mechanic of selecting whatever the other option is. When I went to view the code for my track pad on/off .ps1 script, it was all in a very low level language which ChatGPT said was probably binary mixed with hexadecimal. What on earth?! I know that isn't what I wrote. How did it transform into this?
I would include the code here but it has 1,644 lines of text. This made me wonder if ChatGPT could decode it. After all, its run on a computer that knows how to do all sorts of stuff. In fact, I wonder if somehow it could tell me what Jack's Goose Script was compiled from. I tried that, no, it couldn't tell me much. I guess that ChatGPT would have to have a module like a compiler/decompiler installed in order to do that.
And then I realized the shortcut icon I was clicking on for my trackpad on/off script was itself the shortcut icon which I had then copied to my desktop. I thought the icon on the desktop pointed to the file that looked compiled even tough it was just a .ps1. But then realized it started in the Documents folder and so I checked it and sure enough, my Trackpad script is:
if ((Get-PnpDevice -InstanceId "HID\VID_258A&PID_000C&MI_01&COL01\7&23463051&0&0000").Status -eq "OK")
{
Get-PnpDevice -InstanceId "HID\VID_258A&PID_000C&MI_01&COL01\7&23463051&0&0000" | Disable-PnpDevice -Confirm:$false
}
else
{
Get-PnpDevice -InstanceId "HID\VID_258A&PID_000C&MI_01&COL01\7&23463051&0&0000" | Enable-PnpDevice -Confirm:$false
}
Okay, well at least I can reproduce the trackpad script if needed. But Jacks Anti Goose script is still lost to eternity. I have the script, I can use it on any windows 10 or 11 computer, but I can't change it to do what I want. His script was designed so anytime someone typed goose into the keyboard, the keylogger would immediately shut down the computer, so the daytime class students using that computer couldn't install the annoying Goose script that would place geese everywhere all over the screen, block what you were doing, steal the mouse and drag it across the screen. He destroyed their ability to install it on that computer because he created a compiled script in a language he said he learned just enough to write this script and then immediately uninstalled the compiler, forgot how to use it and even what language it was, but to make sure it always ran on the computer during the time this class was in session, he put it in task scheduler to activate an hour before class or whenever the computer was booted between the time of the class starting and ending and named the task something to do with the Edge browser so the other class couldn't just stop it from running this script.
Anyway, anytime I wrote a script, even with ChatGPTs help, I still had no idea what I was doing but I had done this with Doug's help enough times that I am starting to be able to do this without his help, albeit with a good deal of troubleshooting. And it also helps that I told ChatGPT how best to help me by telling it how to optimize the responses it gives me when troubleshooting. I also put every return I got or whatever the result was into ChatGPT so it could tell me what the problem was.
So I had done a search for powershell script to change audio device with one click, and found this script:
# Requires the AudioDeviceCmdlets module
Install-Module -Name AudioDeviceCmdlets -Force
# Get the names of your audio devices
Get-AudioDevice -List | Select-Object Name
# Replace 'DeviceName1' and 'DeviceName2' with the actual names of your devices
$device1 = "DeviceName1"
$device2 = "DeviceName2"
# Function to switch to the other device
function Switch-AudioDevice {
$currentDevice = (Get-AudioDevice -Playback).Name
if ($currentDevice -like "*$device1*") {
(Get-AudioDevice -List | Where-Object Name -like "*$device2*") | Set-AudioDevice
} else {
(Get-AudioDevice -List | Where-Object Name -like "*$device1*") | Set-AudioDevice
}
}
# Create a shortcut to run the script with a single click
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\SwitchAudio.lnk")
$Shortcut.TargetPath = "powershell.exe"
$Shortcut.Arguments = "-ExecutionPolicy Bypass -File ""$PSScriptRoot\SwitchAudio.ps1"""
$Shortcut.Save()
I ran it through ChatGPT and it had me install AudioDeviceCmdlets, and I had it explain every part of the script as well. I had it compare the one I found online with the one ChatGPT created before this and ChatGPT suggested the one I found online for being more concise and easier to read.
But the script I found online was limited to two devices. I have three...but I only use two. As a matter of fact the portable monitor I use at home for my laptop has speakers and hijacks my audio every time I plug it in ad this is annoying. I have no use for it. My laptop speakers sound better than the speakers that are in the monitor. The monitor speakers are useful in the sense that if I had device with no speakers of its own, then the screen as speakers, but that's not the case here. I only use the Realtek device when just using the laptop and only the FiiO device DAC connected to my sound system when at home. So I inserted the names of both devices into the script and then saved the script as a .ps1 and ran it.
I noticed the trackpad script had a different icon and I tried to activate the script I just created to make sure it worked and it didn't, instead it opened the script in notepad and then I remembered why the icon for the trackpad script was different. PowerShell scripts always just open in notepad or whatever editor when double-clicked. How you get them to activate instead is either by right-clicking and selecting Run in PowerShell, or creating a shortcut and mapping it to the .ps1 script. So I did that. I had to put the script where I wanted to keep it before doing this, and then I mapped it. And I made sure to not only copy and paste the file path exactly to the shortcut icon by right clicking for context menu and going properties and under the shortcut tab, under the Target field, pasting that file path, but also I checked my trackpad script and saw that the name of the file was included in it because for a second I wasn't sure, so I then copied the name of the script which I had named "ToggleAudioDvcs" and included that in the file path for the shortcut and ended it with the file extension, .ps1 with an ending quotation mark and finished.
I ran the script, it activated like a batch script by automatically opening in PowerShell (not Command Prompt because batch would just run in Command Prompt), but it didn't change the audio device. ChatGPT had me verify installation of the AudioDeviceCmdlets module:
Get-Command -Module AudioDeviceCmdlets
It returned the below text and suggested I should see commands like Get-AudioDevice and Set-AudioDevice listed.:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-AudioDevice 3.1.0.2 AudioDeviceCmdlets
Cmdlet Set-AudioDevice 3.1.0.2 AudioDeviceCmdlets
Cmdlet Write-AudioDevice 3.1.0.2 AudioDeviceCmdlets
ChatGPT also suggested I enable script execution but I already knew it was enabled because I had already done this for the trackpad script which does also use PowerShell, incase I didn't already mention that.
I told ChatGPT that AudioDeviceCmdlets is installed and I verified it after I installed it. So it suggested I enter the following command:
Get-AudioDevice -List
It gave me this output:
"Index : 1 Default : True DefaultCommunication : True Type : Playback Name : Speakers (FiiO K5 Pro) ID : {0.0.0.00000000}.{a305e7cb-cbd4-46ef-b416-1e420932516d} Device : CoreAudioApi.MMDevice Index : 2 Default : False DefaultCommunication : False Type : Playback Name : N1F PRO (2- HD Audio Driver for Display Audio) ID : {0.0.0.00000000}.{e0252aad-6806-438c-906c-e9ff489cccfd} Device : CoreAudioApi.MMDevice Index : 3 Default : False DefaultCommunication : False Type : Playback Name : Speakers (Realtek(R) Audio) ID : {0.0.0.00000000}.{e6467254-5fa8-4338-b40a-7cfb9ea8203a} Device : CoreAudioApi.MMDevice Index : 4 Default : True DefaultCommunication : True Type : Recording Name : Microphone (Realtek(R) Audio) ID : {0.0.1.00000000}.{1238472a-1564-4d73-b0b0-7d40e386231f} Device : CoreAudioApi.MMDevice"
ChatGPT said "the output you shared confirms that both devices are available, but maybe the script is not switching the audio device properly due to a mismatch in the logic. The Get-AudioDevice -Playback command shows the current default audio device, but your script might not be switching because it’s not properly checking the device names or switching between the two." It had me replace the line:
Install-Module -Name AudioDeviceCmdlets -Force
and in its place, put:
# Get the names of your audio devices
Get-AudioDevice -List
That made a lot of sense to me because I was looking at it earlier and thought that if it had me install it earlier, then why does the script need to say to install it as well? And doesn't the script actually need to say to import the Cmdlets before it can use them? But this is all based on what I understand about Python from my weather display project. So I don't know. Cross that bridge when we have to. So I thought, okay, well I think that problem is solved and I wasn't even troubleshooting that problem yet. It was fixed as a result of troubleshooting something else.
Then I wondered if maybe the reason the script wasn't working was because in the script, there are quotations marks around the names of the devices. I just had to make sure even though I now quotation marks are used in code all the time to differentiate between the name of something and the code around it. I asked ChatGPT and the answer is no, the quotes are necessary.
Then I had a moment where I remembered a time when Doug was helping me and the script wouldn't run but we would copy the text and run it manually in a terminal and it worked fine. So something was wrong with the fact that we were using a script to activate it or something like that. I recalled us copying and pasting the text directly into the terminal so I opened the script again and copied all the text, ran it in PowerShell, and to my surprise, it worked. The audio device had changed from what I recalled it being earlier. I ran it again to make sure and I got reliably reproducible results. Okay, so now why isn't it working when activating the script? Doug and I did solve this problem before but I don't know what we did. I entered the text into ChatGPT to show that it worked fine:
Get-AudioDevice -List Index : 1 Default : True DefaultCommunication : True Type : Playback Name : Speakers (FiiO K5 Pro) ID : {0.0.0.00000000}.{a305e7cb-cbd4-46ef-b416-1e420932516d} Device : CoreAudioApi.MMDevice Index : 2 Default : False DefaultCommunication : False Type : Playback Name : N1F PRO (2- HD Audio Driver for Display Audio) ID : {0.0.0.00000000}.{e0252aad-6806-438c-906c-e9ff489cccfd} Device : CoreAudioApi.MMDevice Index : 3 Default : False DefaultCommunication : False Type : Playback Name : Speakers (Realtek(R) Audio) ID : {0.0.0.00000000}.{e6467254-5fa8-4338-b40a-7cfb9ea8203a} Device : CoreAudioApi.MMDevice Index : 4 Default : True DefaultCommunication : True Type : Recording Name : Microphone (Realtek(R) Audio) ID : {0.0.1.00000000}.{1238472a-1564-4d73-b0b0-7d40e386231f} Device : CoreAudioApi.MMDevice PS C:\Users\Christian> PS C:\Users\Christian> # Get the current audio device PS C:\Users\Christian> $currentDevice = (Get-AudioDevice -Playback).Name PS C:\Users\Christian> Write-Host "Current device: $currentDevice" Current device: Speakers (FiiO K5 Pro) PS C:\Users\Christian> PS C:\Users\Christian> # Define the device names you want to switch between PS C:\Users\Christian> $device1 = "Speakers (Realtek(R) Audio)" PS C:\Users\Christian> $device2 = "Speakers (FiiO K5 Pro)" PS C:\Users\Christian> PS C:\Users\Christian> # Check if the current device matches device1 PS C:\Users\Christian> if ($currentDevice -like "*$device1*") { >> Write-Host "Currently using $device1, switching to $device2" >> (Get-AudioDevice -List | Where-Object Name -like "*$device2*") | Set-AudioDevice >> } else { >> Write-Host "Currently using $device2, switching to $device1" >> (Get-AudioDevice -List | Where-Object Name -like "*$device1*") | Set-AudioDevice >> } Currently using Speakers (FiiO K5 Pro), switching to Speakers (Realtek(R) Audio) Index : 3 Default : True DefaultCommunication : True Type : Playback Name : Speakers (Realtek(R) Audio) ID : {0.0.0.00000000}.{e6467254-5fa8-4338-b40a-7cfb9ea8203a} Device : CoreAudioApi.MMDevice PS C:\Users\Christian> PS C:\Users\Christian> # Check if the device was switched successfully PS C:\Users\Christian> $currentDevice = (Get-AudioDevice -Playback).Name PS C:\Users\Christian> Write-Host "After switching, current device: $currentDevice" After switching, current device: Speakers (Realtek(R) Audio)"
ChatGPT said it seems that the script worked perfectly when run directly in PowerShell, meaning that it successfully detected the current audio device, switched between the two devices, and confirmed the switch. It suggested there was probably a problem with the shortcut, and maybe it isn't leading to the file properly or there was some other minor detail. But when I click on I right click on the actual script and run in PowerShell, it still doesn't run, that totally bypasses the shortcut. Plus, I specifically and carefully copied and pasted the file path to the actual script from file explorer directly to the shortcut and copied the name, backlashed and added the name and the file extension of the file with ending quotation marks included. I even made sure those quotes were supposed to be there. So it is not the shortcut. I told GPT that the problem was not with the shortcut, and I explained why.
It thanked me for the clarification, and said that if the script works fine when I copy and paste it directly into PowerShell but shows red text (indicating errors) when running the script via right-click → Run with PowerShell, this suggests that the issue may lie with how PowerShell is handling the script execution in that context.
I thought, well, there is a bunch of red text on the screen in the PowerShell window that probably isn't supposed to be there if everything was working fine. So I'll feed it into ChatGPT and see what it thinks. But I had to get the window to stay open to I could copy the text, and when I activate the script, PowerShell opens real quick, flashes all sorts of text and quickly disappears again.
ChatGPT said that red text typically indicates an error or warning message, while normal output is usually in black or white, depending on the theme. "If the entire script output is red, it likely means there’s an issue with the script execution." So I needed to copy the text and put it in ChatGPT but I didn't know how to do this. I knew it could be done because just about every problem I am solving here, Doug had to help me solve with the trackpad script. But I don't remember how he did it and instead of looking at that script and struggling to compare, I ChatGPT could do it. But to get the PowerShell window to stay open so I can copy the red text, I asked ChatGPT what to do and and the end of the script, so to add a pause to the script at the end I should try adding:
Read-Host "Press Enter to continue..."
Then I copied everything in the PowerShell window including the red text and pasted it in ChatGPT:
ChatGPT said the error message I was encountering indicates that the Get-AudioDevice cmdlet is not recognized. This usually happens when the AudioDeviceCmdlets module is not installed or properly imported into the session.
Get-Module -ListAvailable
I had already told it that it was definitely installed properly and shared the results with it. So based on the error output, it seems that the module may not be loaded when running the script. And now we cross the bridge because we have to, the problem was that firstly, it was already installed and secondly, it was not being imported into the script, like Python needed at the beginning of every script that used external resources. It had me add the following to the top of the script above the other top line that was already replaced earlier with Get AudioDevice -List:
Import-Module AudioDeviceCmdlets
ChatGPT said that this error message indicates that PowerShell cannot find the AudioDeviceCmdlets
module. This is likely because the module is not in the expected module path or the system can't locate it when running the script. It suggested I then re-edit that top line I just added from:
Import-Module AudioDeviceCmdlets
to include the explicit file path instead:
Import-Module "C:\Path\To\Module\AudioDeviceCmdlets"
ChatGPT said to enter the following in order to obtain the explicit file path to enter into the quotation marks for this updated new top line:
Get-Module -ListAvailable AudioDeviceCmdlets
When it returned the path, I copied it and pasted it into the top line of the script between the quotation marks in place of the placeholder file path that ChatCPT provided for context, and then I saved the script again. I told ChatGPT this seems to have fixed it. But it suggested that for good measure, I then add this line to the top, now above everuything else:
$env:PSModulePath += ";C:\Users\Christian\Documents\PowerShell\Modules"
It said I could also install the module globally, but I didn't feel like that was necessary. So now, the completed script:
$env:PSModulePath += ";C:\Users\Christian\Documents\PowerShell\Modules"
Import-Module "C:\Users\Christian\Documents\PowerShell\Modules\AudioDeviceCmdlets"
# List all audio devices
Get-AudioDevice -List
# Get the current audio device
$currentDevice = (Get-AudioDevice -Playback).Name
Write-Host "Current device: $currentDevice"
# Define the device names you want to switch between
$device1 = "Speakers (Realtek(R) Audio)"
$device2 = "Speakers (FiiO K5 Pro)"
# Check if the current device matches device1
if ($currentDevice -like "*$device1*") {
Write-Host "Currently using $device1, switching to $device2"
(Get-AudioDevice -List | Where-Object Name -like "*$device2*") | Set-AudioDevice
} else {
Write-Host "Currently using $device2, switching to $device1"
(Get-AudioDevice -List | Where-Object Name -like "*$device1*") | Set-AudioDevice
}
# Check if the device was switched successfully
$currentDevice = (Get-AudioDevice -Playback).Name
Write-Host "After switching, current device: $currentDevice"
# Your script code here
Read-Host "Press Enter to continue..."
I should add that the two above lines with the included text "# Your script here" and "Read-Host "Press Enter to Continue..."" are meant for troubleshooting in this particular case. If you want the PowerShell window to disappear after the script has done what you wat, then you need to remove these two lines and save it again, which I have done.
This has been Truncat3d 00000000111100010100110______________end of line