2

I have a Python Script to enable/ disable Airplane mode using input simulation which works fine:

  • Win + A : To Open Action Center
  • Right Arrow (twice) : Navigate to Airplane toggle
  • Space : To Press OK
  • Win + A : To Leave Action Center
import pyautogui
pyautogui.hotkey('win', 'a')
pyautogui.press('right', presses=2)
pyautogui.press('space')
pyautogui.hotkey('win', 'a')

I tried doing that using PowerShell:

$shell = New-Object -ComObject WScript.Shell
$shell.SendKeys("^{ESC}")
$shell.SendKeys("{RIGHT}")
$shell.SendKeys("{RIGHT}")
$shell.SendKeys(" ")
$shell.SendKeys("^{ESC}")

However, it doesn't seem to work.
Because $shell.SendKeys("^{ESC}") open Start Menu, not Action Center what I want.

What is the correct command for that? Win + A

ap

2 Answers 2

3

By following mklement0's answer, I created a WinKey function to make it easy to use and call, building on code from this answer, which in turn is based on this C# answer.

function WinKey {
    param ([string]$plus)

    $Definition = @'
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class SendKey {
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        private const int KEYEVENTF_EXTENDEDKEY = 1;
        private const int KEYEVENTF_KEYUP = 2;

        public static void SendKeysWithWinKey(string keys = null) {
            keybd_event((byte)Keys.LWin, 0, KEYEVENTF_EXTENDEDKEY, 0);
            if (!String.IsNullOrEmpty(keys)) {
                SendKeys.SendWait(keys.ToLowerInvariant());
            }
            keybd_event((byte)Keys.LWin, 0, KEYEVENTF_KEYUP, 0);
        }
    }
'@

    Add-Type -TypeDefinition $Definition -ReferencedAssemblies "System.Windows.Forms"
    [SendKey]::SendKeysWithWinKey($plus)
}

# Examples usage:
WinKey   # Start Menu
WinKey A # Action Center
WinKey = # Magnifier

For Airplane switch (Win 11):
The position of airplane toggle maybe different on other PCs. Customize it yourself.

$shell = New-Object -ComObject WScript.Shell
WinKey A
$shell.SendKeys("{RIGHT 2}")
$shell.SendKeys(" ")
WinKey A

I don't know whether PowerShell has its own feature or not, without using C# code.
Still waiting for another possible answer with pure powershell without calling other programming languages ​​(C#/python/etc)

1
  • 1
    Nice wrapper function. As for a PowerShell-native solution: unfortunately, P/Invoke solutions (i.e. calls to Win32 APIs) invariably require using ad hoc-compiled C# code via Add-Type.
    – mklement0
    Commented May 30 at 21:35
2

GUI-scripting, i.e. programmatic simulation of interactive user input, is always brittle (can situationally fail), so it's always better to look for other solutions:

  • This answer shows C++ code for toggling airplane mode, which can be compiled to a console application that you could call from PowerShell.

  • This SU answer shows a wmic.exe approach that approximates turning airplane mode on or off by enabling or disabling the Wi-Fi network adapter, which therefore doesn't cover all wireless hardware, notably not Bluetooth; also note that it requires elevation (running as administrator); for syntax reasons, a slight tweak is required in PowerShell:

    # REQUIRES ELEVATION
    # Disables the Wi-Fi adapte; to enable, use "enable"
    wmic.exe path win32_networkadapter where --% NetConnectionID="Wi-fI" call disable
    

If you cannot avoid a GUI-scripting solution:

  • Unfortunately, you cannot send key chords based on the WinKey (Windows Key) via (New-Object -ComObject WScript.Shell).SendKeys() (nor via [System.Windows.Forms.SendKeys]::SendWait()).

  • The SU answer linked to above offers a GUI-scripting approach that doesn't require a WinKey keypress; adapted to PowerShell (untested):

     # Start the Settings app and open the relevant page.
     Start-Process ms-settings:network-airplanemode
     # Give the app some time to open.
     Start-Sleep -Milliseconds 1000 
     # Send a <space> char. to toggle, then Alt+F4 to close the window.
     (New-Object -ComObject WScript.Shell).SendKeys(" %{F4}")
    
    • The downside is that this approach is visually more disruptive and slower than what you attempted to do.
  • Otherwise:

    • If you can assume that Python is installed, call your Python code via the Python CLI (you must also ensure that module pyautogui is installed):
      python -c 'import pyautogui; pyautogui.hotkey('win', 'a'); ...'

    • Otherwise or alternatively, compile a helper class that uses P/Invoke to allow sending WinKey-based key chords: see this answer.

4
  • This SU answer shows a wmic.exe... Its not really airplane switch. Commented May 29 at 20:07
  • Is possible to run Start-Process ms-settings:network-airplanemode minimized/ hidden, and worked? Commented May 29 at 20:11
  • 1
    @OprexDroid, no: Since this method simulates what a user would do interactively, you can't run the Settings app hidden. For invisible operation, you'll need one of the first two solutions.
    – mklement0
    Commented May 29 at 20:25
  • @OprexDroid, as for the wmic.exe solution: that's why I said that it emulates toggling airplane mode, but I've updated the answer to make that clearer.
    – mklement0
    Commented May 29 at 20:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.