Search code examples
powershellsendkeysgetdate

Get Minutes and output them with SendKeys


this is my first time scripting, and I wanted to start small. My goal is simple: get the current time (Get-Date -Format mm) and output that 2 digit number as keystrokes with SendKeys.

Problem is, I have no idea how to convert that 2 digit output into an object for the "SendKeys" to output.


Solution

  • Convert the Datetime results first to String using the ToString() Method if you have some problem to send it, here's the full example to send it to notepad:

    ## Find all Active Windows Titles
    $windows=Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle
    ## Find Specific name 
    $WindowTitle=($windows | ? {$_ -match "Notepad"} ).MainWindowTitle
    ## Add Type and Focus Activate the Window
    $wshell = New-Object -ComObject wscript.shell
    $wshell.AppActivate($WindowTitle)
    ## Send Keys
    $wshell.SendKeys((Get-Date -Format mm).ToString())