PowerShell - Sendkeys

preview_player
Показать описание
(How to send keyboard input with PowerShell to any open window or application)
In this video I demonstrate a practical use case for using sendkeys in a PowerShell script. You can use sendkeys to automatically click a button, or confirm a prompt for instance.

add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
[Microsoft.VisualBasic.Interaction]::AppActivate()
[System.Windows.Forms.SendKeys]::SendWait()

*powershell
*learn powershell
*automation
*learn automation
*windows
*windows powershell
*automatic installations

Code:
Demo1
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

start-sleep -Milliseconds 2500

[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("+{h}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{e}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{l}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{l}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{o}")

Demo2
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

Start-Sleep -Milliseconds 100
[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad")

$line = "some line to write to notepad" #this can also come from a file or database

foreach ($c in $line.GetEnumerator()){
[System.Windows.Forms.SendKeys]::SendWait("{$c}")
Start-Sleep -Milliseconds 10
}

Demo3
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

start-sleep -Milliseconds 2500

[Microsoft.VisualBasic.Interaction]::AppActivate("Computer Management")
start-sleep -Milliseconds 1000

[System.Windows.Forms.SendKeys]::SendWait("{DOWN 3}")
start-sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("{RIGHT 2}")
start-sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("{DOWN 1}")
start-sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("{RIGHT 1}")
start-sleep -Milliseconds 1000
[System.Windows.Forms.SendKeys]::SendWait("{DOWN 2}")

Demo4
add-type -AssemblyName microsoft.VisualBasic

start-sleep -Milliseconds 5000

[Microsoft.VisualBasic.Interaction]::AppActivate("Yes or no")
start-sleep -Milliseconds 1000

[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

Installer hangs script

Add-Type -AssemblyName System.Windows.Forms
$useip = [System.Windows.Forms.MessageBox]::Show("Do you want to provide a static IP?","Yes or no",4)
if ($useip -eq "Yes"){
#$ipnumber = Read-Host "enter last octect 1-255 is valid : $network_prefix"
}

Write-Output "and now we are done"
Рекомендации по теме
Комментарии
Автор

Finally, the explanation I have been looking for while I am learning powershell! Thank you!

lpgm
Автор

you don`t need using delay.. you should check the windows is run and active ..its going to me much faster.Great video - thanks.

phantomaticscience
Автор

Very cool. I already knew about WScript.Shell sendkeys method but this is more useful for sure. I will definitely put this to work.

djust
Автор

Thank you so much 🙏🙏🙏 your script help me a lot

shalinipandit
Автор

Thanks for sharing.. Learning alot from your videos.

eechaze
Автор

The video was simple, easily to follow and very helpful. Is it possible to run this in the background? For example if we want to have multiple things doing at the same time. Having this automation run in the background while playing a video game at the same time.

ShayanKhan-tjle
Автор

Wow didn't know powershell could do this 🤯

MuhammadBerki
Автор

I am not able to do it in poweshell ide could you please explain why you used visual studio instead of powershell ISE

Pavankumar
Автор

i have default powershell, can i do this?

knowledgeanddocumentationm
Автор

How to pass control+N in powershell script???

Pavankumar
Автор

Hi Mr Automation! Thanks for your video. Can we have the script for installer-hangs please ?

DigitalInfogerance
Автор

Demo 3: only opening "Computer Management", but not move any arrow keys nor expanding. Seems some error.

manasdash
Автор

Alright, so we have the code for sending the keys... but how does one activate these scripts in the context of automation within the applications we're using them for? So, we wouldn't always open Visual Studio or Powershell while using the apps we want to automate 'cause that would eliminate the whole reason for having them, so if the scripts are made- how do we implement the scripts as we're performing our tasks? Are they triggered, do they have hotkeys to execute them, or...

sbandc
Автор

Do you know why example 4 is not working for me, it wont type Enter but when i tried in in notepad it working?

quinnvanhest
Автор

Very nice and educational video. Is it possible to write to a UAC prompt, auto fill user and password?

KnifeEye
Автор

Can you please share any url or link to learn more about this.
I was trying to install an exe but it works on some systems and i am facing timing issue between sendkey event.

itsupportbyharshit
Автор

How could I send the keys win + d without using notepad?, thanks in advance!

grankenk
Автор

Is it possible to use this to send keystroke to another computer in my network? Trying to highlight the app I'm trying to start and sending an {Enter} command to press "Start" (Have successfully managed to start the app by sending a "StartScheduledTask" command to it)

Davidp
Автор

Lame examples can also be good examples. ;)

enzym