Skip to content
Back to skills

Windows Desktop Automation

ASecurity

Code spawning CLIs/SendKeys on Windows? Exact argv, focus.

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 21, 2026
ai-agentspythongoshellbashnodegit

Works with

  • terminal
  • cli

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add Rafaelrr5/agent-cortex --skill windows-desktop-automation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Windows Desktop Automation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Windows Desktop Automation
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/rafaelrr5-windows-desktop-automation/badge)](https://www.skillsdirectory.com/skills/rafaelrr5-windows-desktop-automation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: windows-desktop-automation
description: "Code spawning CLIs/SendKeys on Windows? Exact argv, focus."
version: 1.0.0
author: Rafael with AI assistance
license: MIT
platforms: [windows]
---

# Windows desktop automation from code

For code (Node, Python, etc.) that opens a CLI in a new PowerShell window, passes it
arbitrary text (prompts, paths), or types into an existing window with clipboard +
`WScript.Shell.SendKeys`. Typical bugs: prompts that arrive split or empty, flags that eat
the positional argument, and keystrokes that land in the wrong window when several jobs
run together.

Hidden-window and console-flash problems belong to a separate hidden-process diagnostic workflow.

## When to Use

- Prompts or paths reach a spawned CLI split, missing quotes, or reported as "no prompt
  provided", especially when the text contains `"` or when an attachment/extra-dir flag
  is present.
- A scheduler or bot types into existing windows, and text lands in the wrong window when
  several targets, or several jobs at the same minute, are handled.
- You're writing a new tool that generates .ps1 launchers or uses clipboard + SendKeys.
- An agent has to act on the user's live desktop unattended and must first know the
  desktop is usable.
- You need a screenshot of one specific window as evidence.
- The user wants a desktop shortcut that opens a terminal already running a CLI in every
  tab: see `references/terminal-launcher-shortcuts.md`.
- A file will not delete ("in use"), or vendor software runs that the user never
  installed: see `references/locked-files-and-bundled-software.md`.

## Procedure

1. **Reproduce at the argv boundary before reading more code.** Swap the target exe for a
   stand-in that dumps argv (`node -e "console.log(JSON.stringify(process.argv.slice(2)))"`)
   and feed it the exact generated script and text. For the real CLI, run once in its
   non-interactive mode (`-p`, `--print`) with `--help` open. A "missing argument" error
   means a flag took the positional as a value.
2. **Check which PowerShell and which binary actually run.** Look at `$PSVersionTable`
   (5.1 is the default) and at `Get-Command tool` / `where tool`. It may be a `.ps1`/`.cmd`
   npm shim rather than the `.exe`.
3. **Fix argument passing** with the recipe below. Verify with the argv dump against a
   hostile corpus.
4. **Fix window typing**: add a focus guard, deliver to existing windows before spawning new
   ones, and serialize delivery globally.
5. **Lock it in with offline tests** that run the real generated PowerShell with the desktop
   calls stubbed (see reference). Then do one smoke run against the real exe.
6. Report what was verified against the real binary versus the stubs, and say plainly what
   stayed unverified (real SendKeys into a live window takes over the user's desktop, so
   don't do it unasked).

## Rules

- **Windows PowerShell 5.1 does not escape embedded `"` in native-command arguments.**
  `& exe $text` splits any text containing quotes, so any prompt with JSON-quoted paths
  breaks. Build the command line in the host language with MSVC/`CommandLineToArgvW`
  quoting. Pass it through the stop-parsing token from an env var:
  `& 'C:\full\tool.exe' --% %TOOL_ARGS%`.
- **Invoke the real `.exe` by full path, never the npm `.ps1`/`.cmd` shim.** Shims re-splat
  `$args` / `%*` and re-break the quoting. Resolve from PATH (including
  `<prefix>\node_modules\@scope\pkg\bin\tool.exe`) and offer an override env var.
- **Put `--` before a positional argument that follows a variadic option** (e.g.
  `--add-dir <dirs...>`). The parser otherwise takes the positional as one more value.
- **Check the 32767-char command-line cap** before launching. Fail with a clear error and
  point to a file or attachment for long content.
- **Generated .ps1 files: UTF-8 with BOM** (PS 5.1 reads BOM-less files as ANSI). Quote every
  interpolated value as a single-quoted literal (`'` → `''`). Use `-LiteralPath` for paths.
  Delete temp files at the top of the script, because code after an interactive TUI only
  runs on exit.
- **SendKeys types into whatever window is in front.** `AppActivate` returning true isn't
  enough. Compare `GetForegroundWindow()` with the target `MainWindowHandle` immediately
  before the paste and before Enter, and abort with a one-line error if they differ.
- **Within one batch, type into existing windows before opening new ones.** New consoles take
  focus asynchronously. Keep reported results in input order.
- **Clipboard and focus are global:** jobs that fire in the same tick must run through one
  serial promise chain/queue. Chain with `.catch` so one failure doesn't stall the rest.
- **Show users the first line of PowerShell errors and log the full record.** ErrorRecord
  dumps are unreadable in a UI.
- **Screenshot one window with `PrintWindow`, not `CopyFromScreen`.** A screen copy grabs
  whatever is on top, and a minimized window yields a title-bar-sized bitmap. Restore it
  without activating, capture with `PW_RENDERFULLCONTENT` (flag 2, needed for GPU-rendered
  windows), then re-minimize. `scripts/capture_window.ps1 -ProcessId <pid>` does all of it.
  Pass it native `C:/...` paths: git-bash `/tmp` paths do not exist for `powershell -File`.
- **From git-bash, kill a process tree with `taskkill /PID <pid> /T /F` (single slashes).**
  With MSYS path conversion disabled, the `//PID` escape is rejected as an invalid argument.

## Acting on the user's live desktop

- **Detect a locked screen with `LogonUI.exe` in `tasklist`.** While the screen is locked,
  `OpenInputDesktop` still returns a desktop named `Default` and `GetForegroundWindow` is
  non-zero, so neither proves the desktop is usable.
- **Read idle seconds once, at the start** (`GetLastInputInfo` against `GetTickCount`).
  Locked with idle already past several minutes means the user is away, whatever was said
  earlier. Do the non-GUI preparation, check once more, then stop.
- **Bound the wait and report BLOCKED with the reason** (peak idle seen, lock state)
  instead of looping past the window the user set. Leave prepared files in place so a
  retry starts at the step that needs the desktop.
- **Human-only gates (captcha, login, 2FA): the user acts, the agent finishes the step.**
  Never click the captcha. Fill the rest of the form first, so one tick and one click are
  all that remain. Poll the page's accessibility text read-only and watch the checkbox
  state as well as the page: a ticked reCAPTCHA expires after about two minutes. As soon
  as the box reads checked, press Continue yourself.

## Finding a flashing console

Spawn rules for hidden processes live in the background-process skill. To find which
process flashes a window, poll `Win32_Process` every 300 ms for new PIDs and print name,
parent, grandparent and command line; the culprit is the parent whose child appears just
before each console host. When a script reads `tasklist`, decode with `errors="replace"`:
on localized Windows the "no tasks" message is in the OEM code page (cp850 on some
locales), and a strict UTF-8 decode crashes the reader.

## Support files

- `references/argv-quoting-and-test-harness.md`: the quoting function, the `--%` script
  template, the tested hostile-input corpus, the focus-guard snippet, and how to run the real
  generated PowerShell offline (stubbed cmdlets, node as fake exe, 8.3 path pitfall).
- `references/terminal-launcher-shortcuts.md`: desktop `.lnk` that opens WezTerm with a CLI
  in every tab: dedicated config, portable install, verification over the GUI's socket.
- `references/locked-files-and-bundled-software.md`: a file "in use" that will not delete
  (Restart Manager holder query, leftover handles), OEM-bundled software, UAC uninstall checks.
- `scripts/capture_window.ps1`: evidence screenshot of one window by PID.

## Runnable public anchor

Reference snippets illustrate integration recipes, not standalone tested programs.
Only the following anchor has the verification scope recorded below.

Run `python scripts/test_boundary.py` from this directory. See [verification](VERIFICATION.md) for scope and negative tests.

## Attribution

Prepared by Rafael with AI assistance. Adapted from private procedural notes whose recorded author was hermes-curator. This is not a claim of sole authorship of those notes. Third-party tools retain their own licenses.

Files in this skill

  • SKILL.md5.1 KB
  • VERIFICATION.md687 B
  • references/argv-quoting-and-test-harness.md4.4 KB
  • scripts/test_boundary.py3.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.