Posts

Showing posts from October, 2023

How to Run PowerShell Script on Windows Startup?

Image
  How to Run PowerShell Script on Windows Startup? This is useful when you want to run any automation created using PowerShell on Windows Startup. To run PowerShell script on startup. Create a  Windows Command Script (.cmd file)  i.e. create a file and save it with  .cmd  extension. Write the below command in .cmd file. powerShell path\to\powershell_script.ps1 >> “path\to\log_file.log” script.cmd If you want to run the script in background. Add  -windowstyle hidden  after  powershell . script.cmd Place the file or its shortcut file at below path. C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup Restart the computer  and  you can track its execution in log file.

Powershell code for keypress

 param([int]$Wait) if($Wait -le 0){     $Wait = 60 } function printLogo($Time){     Write-Host "                        .d888  .d888         d8b                                          d88P   d88P           Y8P                                          888    888                                        .d8888b  8888b.  888888 888888 .d88b.  888 88888b.   .d88b.       d88P          88b 888    888   d8P  Y8b 888 888  88b d8P  Y8b      888      .d888888 888    888   88888888 888 888  888 88888888      Y88b.    888  888 888    888   Y8b.     888 888  888 Y8b.            Y8888P  Y888888 888    888     Y8888  888 888  888   Y8888                                                           - cttynul     Timeout set to $Time Seconds            " } printLogo($Wait) While(1) {     [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')     [System.Windows.Forms.SendKeys]::SendWait("{F15}")     Start-Sleep -Seconds $Wait }

Python code for key press

 import win32com.client, time def print_logo(timeout=0):     logo = '''                  ___  ___      _                              / __)/ __)    (_)                   ____ ____| |__| |__ ____ _ ____   ____       / ___) _  |  __)  __) _  ) |  _ \ / _  )     ( (__( ( | | |  | | ( (/ /| | | | ( (/ /       \____)_||_|_|  |_|  \____)_|_| |_|\____)                                     - cttynul'''     print(logo)     if(timeout != 0): print("   Timeout set to " + str(timeout) + " seconds") def main():     timeout=60     print_logo(timeout)     while(1):         shell = win32com.client.Dispatch("WScript.Shell")         shell.SendKeys('{F15}')         time.sleep(timeout) if __name__=="__main__":     main()