Hi A.D.D Gamer, thanks for you comment. This script is doing the same, but in a new style. I am glad you see some value in it ;-) Wscript.Shell is what you use when writing vbs files. (Loooong time ago for me :-P)
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.
Thanks for your message 🙂. You can certainly play a game in the background, but the problem with sendkeys is that it needs to activate a window by title. Which means when you are playing your game and the sendkeys kicks in, it will activate the Window by title and send keys. If you also playing a game this means that the sendkeys could send keystrokes into your game window for instance. Lots of things can be automated in different ways, sendkeys is really your last resort if there is no other mechanism to programmatically instruct what needs to be done. Do you have a specific task you want to run in the background that really needs sendkeys?
@@mrautomation Thank you for the quick response, really appreciate it. I was trying to run 2 different cmd prompts at the same time and was trying to see if it’s possible to run in parallel. But from your comment above, it seems like we can’t do that. Another question besides this, Is there a command that would only send keys when the program asks for the user input? I was thinking not to rely on the start-sleep
Hi @@ShayanKhan-tj6le, you are welcome. You can actually run several cmd prompts (as long as they have a unique title) and use sendkeys to send keystrokes. It is possible to accept user input, but only if the prompt has a unique title(Like in my example with the messagebox). Start-sleep is only in the code for demo purposes(so you can actually see what is happening). If you don't want to use sleep, just make sure you always activate the correct window by title and send as many keystrokes if you want without sleep. There is a tool called AUTOIT, that has its own scripting language, looks like vbscript. I am not sure if the tool is free(havent used it for at least 13 years), but that tool is really awesome to automate anything that needs user input or conformations. It could be worth checking if you really want to do advanced UI stuff in an automated fashion. I hope this helps? Kind regards Mr Automation
I know bro, In one of my videos (I think this one) I also mention that for each time you wish to send a key you first must activate the window, because potentially another window could be active. the sleeps are just for demo purposes so everyone can see what happens. Thanks for you comment though, much appreciated.
Hi Pavan, I am using visual code because I also write python code and C# (although for C# I often switch to visual studio). I am also using visual code, because the PowerShell IDE/ISE does not support newest version of PowerShell on .NET core. Sendkeys does work in ISE for sure, because I have tried it on ISE as well. ISE is for built-in powershell version which ships with Windows. I have installed powershell 7.2 myself (using /NET core), hence you need to use another editor, the built-in editor (ISE) does not support powershell 7. I hope it makes sense. If you check this link, I am using sendkeys from the ISE itself : ruclips.net/video/eeS89X2EMek/видео.html
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...
Hi Starbirth, the videos that I create are often examples of functionalities that can be useful in your day to day administration tasks. To give a practical example for the sendkeys. I have used that in the past for application loadtesting. So what I did was like spin up 250 virtual machines, made sure autologin was enabled, and for that specific user that logs on to the system a powershell script launches at login. The application(s) was/were then started from the powershell script and using sendkeys to actually perform the load test for the specific application. Another example. I am also using it myself whenever I start up my own environment, like starting all the tools in need, connecting to the correct sources and such. Most of the things in Windows you can automate using powershell, but whenever it comes to clicking buttons or filling in forms, sendkeys is there to help you. I hope these 2 examples gave you an impression for the applicability of sendkeys. Kind regards Mr Automation.
@@mrautomation Forgive me, I didn't mean to suggest that the functions weren't useful or practical. I would absolutely love to use them, as I use software that has a disproportionate amount of confirmation windows, pop-ups, prompts, errors, and hi-fives- and automation, per usual, would save me that much time in workflow. My question intended to ask about tasks that wouldn't necessarily be large batch functions to call when initiating a task. This video definitely helps me batch logins and the like, but I was curious about smaller executables within a software after those batches would be run. An example is that if I have a software that I use on multiple devices, I can call a function to log into them simultaneously with credentials and keys, but if in that software I enter information to update a database throughout the day and the information is accompanied by a number of prompts, and those prompts change slightly depending on the kind of data I've entered, if I'm not in Powershell as I'm entering this information, how could I call or trigger a SendKey function to zip through those prompts much faster without interacting with any of them? Would you program a trigger associated with a particular window where the prompts begin, would you save them to a hot-key- therefore diminishing the fullness in automation, or would I need to find a way to build a Powershell environment from which I can interact with the software indirectly? I've been looking for a way to control my applications internally through Powershell, so if so, where to learn to do that would be appreciated as well. Thank you! (Sorry for the long text )
Hi @@sbandc, no worries :-). For your specific scenarios it seems that you perhaps need a small script or several, which always run. The script just checks if a specific prompt is open (based on title) and then answers that accordingly. Running these scripts in the background should not consume any resources if you properly design your while loops (with sleep). Another option would be to investigate AutoIT. I am not sure if its still free. But that tools is really the best there is if you need to automate UI based stuff. I have seen it being used at many customers who did not had the skills to automate application installations, they used AutoIT to click trough the installers, at least until I teached them you could just run most installers silently ;-) or with sendkeys (vbscript at that time). The tricky part with your specific usecase is that the prompt slighty change every now and then. If you cant handle that logic with code, it will be very hard to find the correct button or option with sendkeys.
add-type -AssemblyName microsoft.VisualBasic add-type -AssemblyName System.Windows.Forms start-sleep -Milliseconds 5000 $pic = picture-id $script = $pic.url FUNCTION PICTURE-ID ($GENDER = "MALE"){ $url = new-id | select $_.picture.large; $script = Invoke-Item "url.ps1" | Start-Sleep -milliseconds 1000 [system.Diagnostics.Process]::Start("chrome.exe") } [Microsoft.VisualBasic.Interaction]::AppActivate("Chrome") start-sleep -Milliseconds 5000 [System.Windows.Forms.SendKeys]::SendWait("{$SCRIPT}") start-sleep -Milliseconds 5000 [System.Windows.Forms.SendKeys]::SendWait("{ENTER}") What am I doing wrong? Everything runs normally up until Chrome opens- after which silence from a flashing cursor. I can't get the SendKeys to generate any action.
Hi Quinn, Thanks for your message and sorry for late reply. Please be aware that part 4 depends on the "installer hangs script". This means you first need to run the "installer hangs script" which will open a messagebox asking you to press yes or no. When that script runs, and the messagebox is open, you will run the other piece of code that will actually select that yes/no window (messagebox window) and will simulate an ENTER keystroke which will close the application. I hope this answer helps
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.
There is not much information avialable. If you have timing issues, those are often hard to fix. I know exactly what you are talking about. But still with some code and a while loop you should be able to work around these kind of problems. Make sure you activate the window first before sending a keystoke, best is to do that with every sendkey.
Hi Pavan, you could this: [System.Windows.Forms.SendKeys]::SendWait("^{n}") Please let me know if it works for you. Kind regards Mr Automation. learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?redirectedfrom=MSDN&view=windowsdesktop-7.0#System_Windows_Forms_SendKeys_Send_System_String_
Hi User-ti, I have added it to the comments actually, all the way down it is. This is the installer hangs code: ###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"
@@mrautomation i have a request for video if it already hasn't been made. Can you make a vid about creating a script in PowerShell for a problem from absolute beginning (idea) to finish? Doesn't have to be a complicated problem or long (actually prefer it not be). My main concern is seeing your process. How you go about deciding PowerShell will work for this problem, how you explore and find the cmdlets you need, adding them to script, making it reusable for others, etc. Again whatever you have time for or feel like doing, and if there is any vids like that already please direct me to them thanks.
@Mike Thomas Hi, perhaps this video could help ruclips.net/video/fGpsQ-y8Upo/видео.html ..... But I do see your point in your request, it could certainly be interesting. I am quite sure if you browse all my video's you should be able to find some interesting video's where I start with an empty editor. When I find some time I make a video where I start with an empty editor to solve a simple problem. Thanks a lot for the idea, I am certainly going todo something with that.
@@mrautomation thanks for the reply. I know it is a lot to ask but thanks for at least considering. I literally found your channel yesterday so I have not had time to thoroughly go through the vids. I'll go through them and see if I can find what I'm looking for. Appreciate the help
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)
Hi David1p, thanks for watching the video. It is certainly possible, and the way you want to accomplish this is also possible. When I have some spare time I will create a video on how to send these commands remotely to a system (Scheduled task or run regkey). Its quite tricky because you need to have a usersession available, without a usersession it is not possible to use sendkeys. Thats mainly because non interactive users (like system) have no UI capabilities by default and have no userprofile either. I always use autologon for this purpose, but a scheduled task would do the trick fine as well, taking into account that some usersession needs to exist.
@@mrautomation Hi Mr Automation, just wondering have you created a video regarding this problem? Because I am also facing the same problem as David1p. Thank you in advance :)
Hi @@fatinnabilla3651I did not create a video on this topic yet, but I could create one, I will put it on the list for todo items. It seems more people now asking for it, so I will create a video for this for sure. Stay tuned.
@@mrautomation Thankyouuu so much Mr Automation. I am facing these problems for quite some time already. I really appreciate your time and effort. Your kind gesture to teach people new things really aspired me. Have a nice day ahead :)
Hi Gran, you cant do that with sendkeys, since send keys only works on a per window activation level. If you want to send a global hotkey, you probably need to import some function from [DllImport("user32.dll")]. The kinds of keystrokes you want to send can be access via native windows functions only. I don' t think there is any native .NET class that support sending global keys to the system. At least not the last time I looked at this. You probably need some C# or C++ code for sending global hotkeys to the Operating system. I hope this helps.
Hi Mikkel, sorry I havent responded before, I totally forgot. I am not sure if you can write to the UAC. If the UAC has a windows with a name it should be possible. I havent got any time to really did into this. When I find some time I will try to investigate the possibilities.
Finally, the explanation I have been looking for while I am learning powershell! Thank you!
I am glad you like it, makes it worth to create these video's. Thanks 🙂
Very cool. I already knew about WScript.Shell sendkeys method but this is more useful for sure. I will definitely put this to work.
Hi A.D.D Gamer, thanks for you comment. This script is doing the same, but in a new style. I am glad you see some value in it ;-) Wscript.Shell is what you use when writing vbs files. (Loooong time ago for me :-P)
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.
Is it Start-Job cmdlet?
Thanks for your message 🙂. You can certainly play a game in the background, but the problem with sendkeys is that it needs to activate a window by title. Which means when you are playing your game and the sendkeys kicks in, it will activate the Window by title and send keys. If you also playing a game this means that the sendkeys could send keystrokes into your game window for instance. Lots of things can be automated in different ways, sendkeys is really your last resort if there is no other mechanism to programmatically instruct what needs to be done. Do you have a specific task you want to run in the background that really needs sendkeys?
@@mrautomation Thank you for the quick response, really appreciate it. I was trying to run 2 different cmd prompts at the same time and was trying to see if it’s possible to run in parallel. But from your comment above, it seems like we can’t do that.
Another question besides this, Is there a command that would only send keys when the program asks for the user input? I was thinking not to rely on the start-sleep
Hi @@ShayanKhan-tj6le, you are welcome. You can actually run several cmd prompts (as long as they have a unique title) and use sendkeys to send keystrokes. It is possible to accept user input, but only if the prompt has a unique title(Like in my example with the messagebox). Start-sleep is only in the code for demo purposes(so you can actually see what is happening). If you don't want to use sleep, just make sure you always activate the correct window by title and send as many keystrokes if you want without sleep.
There is a tool called AUTOIT, that has its own scripting language, looks like vbscript. I am not sure if the tool is free(havent used it for at least 13 years), but that tool is really awesome to automate anything that needs user input or conformations. It could be worth checking if you really want to do advanced UI stuff in an automated fashion.
I hope this helps?
Kind regards
Mr Automation
Thanks for sharing.. Learning alot from your videos.
Hi Charles, thank you for your comment and I am very happy to hear that you learned something from my videos :-)
you don`t need using delay.. you should check the windows is run and active ..its going to me much faster.Great video - thanks.
I know bro, In one of my videos (I think this one) I also mention that for each time you wish to send a key you first must activate the window, because potentially another window could be active. the sleeps are just for demo purposes so everyone can see what happens. Thanks for you comment though, much appreciated.
Thank you so much 🙏🙏🙏 your script help me a lot
Hi Shalini, I am glad to hear that it helped you 🙂
I am not able to do it in poweshell ide could you please explain why you used visual studio instead of powershell ISE
Hi Pavan, I am using visual code because I also write python code and C# (although for C# I often switch to visual studio). I am also using visual code, because the PowerShell IDE/ISE does not support newest version of PowerShell on .NET core. Sendkeys does work in ISE for sure, because I have tried it on ISE as well. ISE is for built-in powershell version which ships with Windows. I have installed powershell 7.2 myself (using /NET core), hence you need to use another editor, the built-in editor (ISE) does not support powershell 7. I hope it makes sense. If you check this link, I am using sendkeys from the ISE itself : ruclips.net/video/eeS89X2EMek/видео.html
interessting, thanks for the video
Hi Reda, thanks for your reply and your welcome.
Great Script! Thanks for sharing
I am very happy to hear that :-) And your welcome.
Super helpful. Thank you!!
Hi Justin. I'm glad you like it
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...
Hi Starbirth, the videos that I create are often examples of functionalities that can be useful in your day to day administration tasks. To give a practical example for the sendkeys. I have used that in the past for application loadtesting. So what I did was like spin up 250 virtual machines, made sure autologin was enabled, and for that specific user that logs on to the system a powershell script launches at login. The application(s) was/were then started from the powershell script and using sendkeys to actually perform the load test for the specific application. Another example. I am also using it myself whenever I start up my own environment, like starting all the tools in need, connecting to the correct sources and such. Most of the things in Windows you can automate using powershell, but whenever it comes to clicking buttons or filling in forms, sendkeys is there to help you. I hope these 2 examples gave you an impression for the applicability of sendkeys. Kind regards Mr Automation.
@@mrautomation Forgive me, I didn't mean to suggest that the functions weren't useful or practical. I would absolutely love to use them, as I use software that has a disproportionate amount of confirmation windows, pop-ups, prompts, errors, and hi-fives- and automation, per usual, would save me that much time in workflow.
My question intended to ask about tasks that wouldn't necessarily be large batch functions to call when initiating a task. This video definitely helps me batch logins and the like, but I was curious about smaller executables within a software after those batches would be run.
An example is that if I have a software that I use on multiple devices, I can call a function to log into them simultaneously with credentials and keys, but if in that software I enter information to update a database throughout the day and the information is accompanied by a number of prompts, and those prompts change slightly depending on the kind of data I've entered, if I'm not in Powershell as I'm entering this information, how could I call or trigger a SendKey function to zip through those prompts much faster without interacting with any of them? Would you program a trigger associated with a particular window where the prompts begin, would you save them to a hot-key- therefore diminishing the fullness in automation, or would I need to find a way to build a Powershell environment from which I can interact with the software indirectly? I've been looking for a way to control my applications internally through Powershell, so if so, where to learn to do that would be appreciated as well. Thank you! (Sorry for the long text
)
Hi @@sbandc, no worries :-). For your specific scenarios it seems that you perhaps need a small script or several, which always run. The script just checks if a specific prompt is open (based on title) and then answers that accordingly. Running these scripts in the background should not consume any resources if you properly design your while loops (with sleep). Another option would be to investigate AutoIT. I am not sure if its still free. But that tools is really the best there is if you need to automate UI based stuff. I have seen it being used at many customers who did not had the skills to automate application installations, they used AutoIT to click trough the installers, at least until I teached them you could just run most installers silently ;-) or with sendkeys (vbscript at that time). The tricky part with your specific usecase is that the prompt slighty change every now and then. If you cant handle that logic with code, it will be very hard to find the correct button or option with sendkeys.
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
start-sleep -Milliseconds 5000
$pic = picture-id
$script = $pic.url
FUNCTION PICTURE-ID ($GENDER = "MALE"){
$url = new-id | select $_.picture.large;
$script = Invoke-Item "url.ps1" | Start-Sleep -milliseconds 1000
[system.Diagnostics.Process]::Start("chrome.exe")
}
[Microsoft.VisualBasic.Interaction]::AppActivate("Chrome")
start-sleep -Milliseconds 5000
[System.Windows.Forms.SendKeys]::SendWait("{$SCRIPT}")
start-sleep -Milliseconds 5000
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
What am I doing wrong? Everything runs normally up until Chrome opens- after which silence from a flashing cursor. I can't get the SendKeys to generate any action.
Wow didn't know powershell could do this 🤯
Hi Muhammad, Powershell can do a lot ;-) and thanks for your comment.
Demo 3: only opening "Computer Management", but not move any arrow keys nor expanding. Seems some error.
Hi Manasdash, on my system it does work. Can't really tell why it wont work at your side. Are you debugging the code or just running it?
Wonderful ,Thanks Man .. :)
Hi Manoj, thanks for your message and great that you like it.
i have default powershell, can i do this?
Yes you certainly can, I think this is even supported on the first version of PowerShell which came out in 2006 called Monad.
Do you know why example 4 is not working for me, it wont type Enter but when i tried in in notepad it working?
Hi Quinn, Thanks for your message and sorry for late reply. Please be aware that part 4 depends on the "installer hangs script". This means you first need to run the "installer hangs script" which will open a messagebox asking you to press yes or no. When that script runs, and the messagebox is open, you will run the other piece of code that will actually select that yes/no window (messagebox window) and will simulate an ENTER keystroke which will close the application. I hope this answer helps
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.
There is not much information avialable. If you have timing issues, those are often hard to fix. I know exactly what you are talking about. But still with some code and a while loop you should be able to work around these kind of problems. Make sure you activate the window first before sending a keystoke, best is to do that with every sendkey.
Very helpful video
Thanks. I am glad you like it :-)
How to pass control+N in powershell script???
Hi Pavan, you could this:
[System.Windows.Forms.SendKeys]::SendWait("^{n}")
Please let me know if it works for you.
Kind regards Mr Automation.
learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?redirectedfrom=MSDN&view=windowsdesktop-7.0#System_Windows_Forms_SendKeys_Send_System_String_
Hi Mr Automation! Thanks for your video. Can we have the script for installer-hangs please ?
Hi User-ti, I have added it to the comments actually, all the way down it is. This is the installer hangs code:
###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"
@@mrautomation Hi Mr Automation. Thank you for your feedback. I didn't realize that my question was in the description of the video 😁
@@DigitalInfogerance, Hahha no worries bro, your welcome ;-)
Nice, subbed
Hi Mike. Thanks 👍
@@mrautomation i have a request for video if it already hasn't been made. Can you make a vid about creating a script in PowerShell for a problem from absolute beginning (idea) to finish? Doesn't have to be a complicated problem or long (actually prefer it not be). My main concern is seeing your process. How you go about deciding PowerShell will work for this problem, how you explore and find the cmdlets you need, adding them to script, making it reusable for others, etc. Again whatever you have time for or feel like doing, and if there is any vids like that already please direct me to them thanks.
@Mike Thomas Hi, perhaps this video could help ruclips.net/video/fGpsQ-y8Upo/видео.html ..... But I do see your point in your request, it could certainly be interesting. I am quite sure if you browse all my video's you should be able to find some interesting video's where I start with an empty editor. When I find some time I make a video where I start with an empty editor to solve a simple problem. Thanks a lot for the idea, I am certainly going todo something with that.
@@mrautomation thanks for the reply. I know it is a lot to ask but thanks for at least considering. I literally found your channel yesterday so I have not had time to thoroughly go through the vids. I'll go through them and see if I can find what I'm looking for. Appreciate the help
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)
Hi David1p, thanks for watching the video. It is certainly possible, and the way you want to accomplish this is also possible. When I have some spare time I will create a video on how to send these commands remotely to a system (Scheduled task or run regkey). Its quite tricky because you need to have a usersession available, without a usersession it is not possible to use sendkeys. Thats mainly because non interactive users (like system) have no UI capabilities by default and have no userprofile either. I always use autologon for this purpose, but a scheduled task would do the trick fine as well, taking into account that some usersession needs to exist.
@@mrautomation Hi Mr Automation, just wondering have you created a video regarding this problem? Because I am also facing the same problem as David1p. Thank you in advance :)
Hi @@fatinnabilla3651I did not create a video on this topic yet, but I could create one, I will put it on the list for todo items. It seems more people now asking for it, so I will create a video for this for sure. Stay tuned.
@@mrautomation Thankyouuu so much Mr Automation. I am facing these problems for quite some time already. I really appreciate your time and effort. Your kind gesture to teach people new things really aspired me. Have a nice day ahead :)
@@fatinnabilla3651 Thanks for your nice words :-) A new video incoming about remote sending keys and the caveats around it.
How could I send the keys win + d without using notepad?, thanks in advance!
Hi Gran, you cant do that with sendkeys, since send keys only works on a per window activation level. If you want to send a global hotkey, you probably need to import some function from [DllImport("user32.dll")]. The kinds of keystrokes you want to send can be access via native windows functions only. I don' t think there is any native .NET class that support sending global keys to the system. At least not the last time I looked at this. You probably need some C# or C++ code for sending global hotkeys to the Operating system. I hope this helps.
How would you send a {Space} in this
Hi Slapfrost, .sendkeys(" ") just place a literal space in the sendkeys argument. I hope this helps.
Very nice and educational video. Is it possible to write to a UAC prompt, auto fill user and password?
Hi Mikkel, sorry I havent responded before, I totally forgot. I am not sure if you can write to the UAC. If the UAC has a windows with a name it should be possible. I havent got any time to really did into this. When I find some time I will try to investigate the possibilities.
@@mrautomation thanks for replying, and it's super fine. I found another solution, so there's no need, but I thank you for your intentions 🙏
@1:20 - typo simulation
Lame examples can also be good examples. ;)
:-P