Posted On October 6, 2021

PowerShell: Set Windows Scheduled Task to Send a Pop-up Message

kimconnect 0 comments
blog.KimConnect.com >> Windows >> PowerShell: Set Windows Scheduled Task to Send a Pop-up Message
# Set variables
$taskName='Bi-weekly Meeting Reminder'
$time='11:50am'
$daily=New-ScheduledTaskTrigger -Daily -At $time
$everyOtherDay=New-ScheduledTaskTrigger -Daily -DaysInterval 2 -At $time
$biWeekly=New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Wednesday -At $time
$atLogon=New-ScheduledTaskTrigger -AtLogon

# Command to send a message to user by name
$username='kim'
$command="Send-RDUserMessage -HostServer $env:computername -UnifiedSessionID (query session $username /SERVER:$env:computername|select -skip 1|%{`$_.Split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)})[2] -MessageTitle 'Scheduled Task' -MessageBody '$taskName'"

$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$settings=New-ScheduledTaskSettingsSet -MultipleInstances IgnoreNew -ExecutionTimeLimit 0
#$action=New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-ExecutionPolicy Bypass $scriptFile"
#$action=New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('$taskName')"
$action=New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument $command

# Unregister the Scheduled task if it already exists
Get-ScheduledTask $taskName -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false;

# Create new scheduled task
Register-ScheduledTask -Action $action -Trigger $biWeekly -TaskName $taskName -Settings $settings -Principal $principal

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Remote Desktop Logon Error: ‘the user account used to connect to remote PC did not work

Symptom: The user account did not work error Resolution:In the instance that we've experienced, it…

Analyze BSOD Dump File

Install Debugging Tools for Windows as Standalone Component As Administrator, run CMD cd "C:\Program Files…

User Account Creation Script

1. Create Account in AD a. Select the correct container b. copy memberships of another…