Skip to content

Instantly share code, notes, and snippets.

@mayerwin
Last active July 28, 2025 07:59
Show Gist options
  • Select an option

  • Save mayerwin/7705665d5e15990d0bc0296b9343a597 to your computer and use it in GitHub Desktop.

Select an option

Save mayerwin/7705665d5e15990d0bc0296b9343a597 to your computer and use it in GitHub Desktop.
Schedule Crafty (and optionally Duplicati) to auto‑start at user logon.
<#
.SYNOPSIS
Registers one or more user‐level applications to launch automatically at logon.
.DESCRIPTION
ScheduleAutoStart.ps1 creates Scheduled Tasks under “\ScheduleAutoStart\” for each
entry defined in the `$apps` array. By default it installs a task to launch Crafty
at the current user’s interactive logon. It can optionally be extended (uncomment
the Duplicati line) to auto‑launch additional utilities.
The script self‑elevates to Administrator (if necessary), cleans out any existing
tasks in the `\ScheduleAutoStart\` folder, and then re‑creates them with the latest
definitions. Use this to ensure a fresh install or update of your logon tasks.
.PARAMETER RunAsUser
The user account under which the tasks will run. Defaults to the current Windows
user name (`$env:USERNAME`).
.PARAMETER RunAsDomain
The domain (or computer name) of the user account. Defaults to the current Windows
domain (`$env:USERDOMAIN`).
.EXAMPLE
# Schedule Crafty (and Duplicati, if enabled) for the current user:
.\ScheduleAutoStart.ps1
.EXAMPLE
# Run as a different account:
.\ScheduleAutoStart.ps1 -RunAsUser jdoe -RunAsDomain CONTOSO
.NOTES
• Requires Administrator privileges (the script will prompt for UAC elevation).
• Uses the S4U logon type so no password is stored or prompted.
• Tasks are registered with RunLevel Limited for non‑elevated apps and Highest
for those marked RunHighest = $true.
#>
param(
[string]$RunAsUser = $env:USERNAME,
[string]$RunAsDomain = $env:USERDOMAIN
)
$ErrorActionPreference = 'Stop'
trap {
Write-Host ""; Write-Host ('FAILED: {0}' -f $_.Exception.Message) -ForegroundColor Red
Read-Host 'Press Enter to close'; exit 1
}
# -- Self‑elevate --------------------------------------------------------------
$cur = [Security.Principal.WindowsIdentity]::GetCurrent()
if (-not (New-Object Security.Principal.WindowsPrincipal $cur).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$a = @('-NoProfile','-ExecutionPolicy','Bypass','-File',$PSCommandPath)
$a += '-RunAsUser'; $a += $RunAsUser
$a += '-RunAsDomain'; $a += $RunAsDomain
Start-Process -FilePath "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" -Verb RunAs -ArgumentList $a
exit
}
# -- Constants -----------------------------------------------------------------
$taskUser = "$RunAsDomain\$RunAsUser"
$taskPath = '\ScheduleAutoStart\'
$apps = @(
@{Name='Crafty_AutoStart'; Path="C:\\Users\\$RunAsUser\\Documents\\Crafty\\crafty.exe"; RunHighest=$false}
# Uncomment the next line if you want to auto-launch Duplicati for backup purposes, without having to create a service (buggy and hard to configure properly)
#, @{Name='DuplicatiTray_AutoStart'; Path='C:\\Program Files\\Duplicati 2\\Duplicati.GUI.TrayIcon.exe'; RunHighest=$true}
)
# ── Remove existing tasks ────────────────────────────────────────────────────
Get-ScheduledTask -TaskPath $taskPath -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false -ErrorAction SilentlyContinue
foreach ($app in $apps) {
if (-not (Test-Path $app.Path)) { Write-Warning "Missing: $($app.Path)"; continue }
$act = New-ScheduledTaskAction -Execute $app.Path -WorkingDirectory (Split-Path $app.Path)
$trg = New-ScheduledTaskTrigger -AtLogOn -User $taskUser
$lvl = if ($app.RunHighest) { 'Highest' } else { 'Limited' }
$prn = New-ScheduledTaskPrincipal -UserId $taskUser -LogonType Interactive -RunLevel $lvl
$def = New-ScheduledTask -Action $act -Trigger $trg -Principal $prn -Settings (New-ScheduledTaskSettingsSet -MultipleInstances IgnoreNew)
Register-ScheduledTask -TaskName $app.Name -TaskPath $taskPath -InputObject $def -Force
}
Read-Host 'Press Enter to exit'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment