Skip to content

Instantly share code, notes, and snippets.

@rezarahimian
Created August 18, 2021 18:53
Show Gist options
  • Save rezarahimian/1fab1dd0c086dc848f45b957dbbefc1c to your computer and use it in GitHub Desktop.
Save rezarahimian/1fab1dd0c086dc848f45b957dbbefc1c to your computer and use it in GitHub Desktop.
$Principal = New-ScheduledTaskPrincipal -GroupId 'BUILTIN\Users'
$Command = Join-Path $env:SystemRoot -ChildPath 'System32\WScript.exe'
$Argument = '//NoLogo //B //T:120 "C:\Windows\LogonScript.vbs"'
$TaskAction = New-ScheduledTaskAction -Execute $Command -Argument $Argument
$TaskSetting = New-ScheduledTaskSettingsSet -Compatibility 'Win8' -ExecutionTimeLimit '00:05' -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd
$TaskTrigger = @((New-ScheduledTaskTrigger -AtLogOn), (New-ScheduledTaskTrigger -At '7am' -Daily))
$TaskTrigger | Where-Object { $_.StartBoundry } | ForEach-Object { $_.StartBoundry = [DateTime]::Parse($_.StartBoundry).ToLocalTime().ToString('s') }
$Task = New-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -Settings $TaskSetting -Principal $Principal
$Task.Description = 'Scheduled task to run logon script at user logon and every day at 7am'
Register-ScheduledTask -InputObject $Task -TaskPath '\' -TaskName 'LogonScript'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment