How to connect to a wireless Display adapter automatically through scripts or Task Scheduler?

5,175

Using Brisk's answer I was able to come up with a solution that works for my purposes. I have my PC in another room, but close enough that I can control it with a wireless keyboard. I have a Roku which can be used as wireless display (I think using MiraCast). I wanted to be able to connect my PC to the Roku without having to turn on the monitor in the other room and navigate the menus, just to immediately turn it back off and go back into the bedroom.

The code in Brisk's answer is Visual Basic which can be run by default on any Windows computer. You may need to fiddle with some permissions for Windows to actually allow you to run it. Not sure, because I had already fiddled with some settings for powershell to get those scripts to run using this guide: https://www.addictivetips.com/windows-tips/fix-running-scripts-is-disabled-on-this-system-powershell-on-windows-10/

After I wrote the script, I created a shortcut to run it using: C:\Windows\System32\wscript.exe z:\Scripts\ConnectToRokuStick.vbs Windows shortcuts can easily be assigned keyboard shortcuts in the properties menu. So now I can just press CTRL+ALT+P and which will activate the shortcut, run the script, and connect my PC to my bedroom TV through the Roku.

Below is the code. Save it in a .vbs file and you may need to save it in ANSI format as opposed to UTF. Be aware that copy and pasting code from you browser sometimes results in weird non-standard quotation marks (") which can cause the visual basic compiler to fail with an unrecognized character or other syntax error. If so, just delete them and retype them. It is not necessary to encapsulate this in a Sub as Brisk did.

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%windir%\explorer.exe ms-settings-connectabledevices:devicediscovery"
Wscript.Sleep(1000)
objShell.SendKeys "+{Tab}"
objShell.SendKeys "{Tab}"
objShell.SendKeys "{Enter}"
Wscript.Sleep(1000)
objShell.SendKeys "RokuStick"
Wscript.Sleep(1000)
objShell.SendKeys "{Tab}"
objShell.SendKeys "{Enter}"
objShell.SendKeys "{Esc}"
objShell.SendKeys "{Esc}"

This code calls windows explorer using a shell object with an argument that opens up the connect to wireless display menu. This is equivalent to pressing windows + K on windows 10. It then moves the cursor to the search bar and enters "RokuStick" which is the name of my device. It presses enter to submit the search then and then waits for the search to finish (you may need to add more time). It then moves the cursor down to the device, presses enter and then closes the menu.

Share:
5,175

Related videos on Youtube

Bhavik
Author by

Bhavik

Updated on September 18, 2022

Comments

  • Bhavik
    Bhavik over 1 year

    I am trying to project files to a Samsung TV which has a windows display adapter. I want to automate the entire process. So when the TV is switched on, I want the computer to continuously check for the display adapter and connect automatically. Once its connected I already have it projecting in a duplicate mode which displays files via another batch file running. Could anyone suggest how can I do this through scripts or task scheduler or powershell?

    • DavidPostill
      DavidPostill about 8 years
      Welcome to Super User! Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    • Bhavik
      Bhavik about 8 years
      @DavidPostill : Hey I apologize for the incomplete information. I'm trying to use the following command to firstly test it with my WiFi and then try to go ahead with display adapter on TV. This is the command I'm using netsh wlan connect ssid=Jet Business Loans - Guest name=Jet Business Loans - Guest interface="Wireless Network Connection" and I'm getting this error : One or more parameters for the command are not correct or missing.
  • Scott - Слава Україні
    Scott - Слава Україні about 5 years
    Please explain how this works (what it does) and how to use it.  Please do not respond in comments; edit your answer to make it clearer and more complete.