# Get the directory of the script
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# Define the path to the exe
$exePath = Join-Path -Path $scriptPath -ChildPath "MyExeFile.exe"
# Check if the exe exists
if (-not (Test-Path $exePath)) {
Write-Error "MyExeFile.exe not found in the script directory."
exit 1
}
# Name for the scheduled task
$taskName = "Run Exe File on Login"
# Create a new scheduled task action
$action = New-ScheduledTaskAction -Execute $exePath
# Create trigger to run at system startup
$trigger = New-ScheduledTaskTrigger -AtStartup
# Set up principal to run with highest privileges
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# Create the scheduled task
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -InputObject $task -Force
# Run the task immediately
Start-ScheduledTask -TaskName $taskName
# Get the directory of the script
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# Define the path to the exe
$exePath = Join-Path -Path $scriptPath -ChildPath "MyExeFile.exe"
# Check if the exe exists
if (-not (Test-Path $exePath)) {
Write-Error "MyExeFile.exe not found in the script directory."
exit 1
}
# Name for the scheduled task
$taskName = "Run Exe File on Login"
# Create a new scheduled task action
$action = New-ScheduledTaskAction -Execute $exePath
# Create trigger to run at system startup
$trigger = New-ScheduledTaskTrigger -AtStartup
# Set up principal to run with highest privileges
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# Create the scheduled task
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -InputObject $task -Force
# Run the task immediately
Start-ScheduledTask -TaskName $taskName