What is the powershell code to simulate pressing the windows key?

5.3k Views Asked by At

I'm trying to make a .ps1 program to auto-refresh a site in edge - and I also want it to position the window. I need it to simulate pressing Win+RIGHT but I cant find the code for the windows key.

$wshell.SendKeys('{??????}')
3

There are 3 best solutions below

0
William Higgs On

Personally, if I were you, I would use the Autoit powershell module to accomplish this task. If you download the zip file, you will want to extract the directory "install\AutoitX" in the zip file to some location on your computer. Be sure to unblock the zip file before you do, of course. Then, import the autoit powershell module, and you can send emulate pressing the keys you mentioned with the following code:

Initialize-AU3
Send-AU3Key -Key "#{RIGHT}" -Mode 0
0
postanote On

The keybinding to use with SendKeys for the Windows Key is this:

# Activating the Windows Key
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('^{ESC}')
0
user18432 On

The AutoIt answer works as expected as SendKeys is able to press the windows key but not windowskey + another key.

Download the AutoIt zip and save the AutoItX folder to a permanent location:

https://www.autoitscript.com/site/autoit/downloads/

Example code to press windowskey + e:

Import-Module "C:\Path\AutoItX\AutoItX.psd1"

Initialize-AU3

Send-AU3Key -Key "{LWINDOWN}e{LWINUP}" -Mode 0

(I would have posted this as a comment to the existing AutoIt answer but I don't have the 50 reputation yet, sorry)