Hi everyone, I’m Jordan from PDQ. Today, we’re going to explore how to create scheduled tasks using PowerShell or leverage Group Policy to accomplish the same goal. This is especially useful when you need to run actions against machines that require a user’s profile — something that can be tricky with a standard deployment.
When to use Group Policy vs. scheduled tasks
Use Group Policy if possible
If you have access to Group Policy, use it. Group Policy is fantastic at managing user environments and running logon or logoff scripts, including PowerShell scripts. Since these scripts are stored and managed centrally, you don’t have to track them across multiple machines.
Group Policy works best when you want to:
Run scripts at logon or logoff that need to affect the user’s profile.
Maintain central control and monitoring of user-related tasks.
Avoid the need for custom scheduling or task creation.
But, not everyone has access to Group Policy. If that’s the case, PowerShell can help you achieve a similar outcome by creating scheduled tasks.
Using PowerShell to create scheduled tasks
For those who can’t use Group Policy, creating a scheduled task with PowerShell is the next best option. PowerShell’s command structure is straightforward, with commands following the pattern:
Verb-Noun
To list available commands related to scheduled tasks, you can use:
Get-Command *ScheduledTask*
Key PowerShell commands for scheduled tasks
New-ScheduledTaskAction
:
Defines what the task will do.New-ScheduledTaskPrincipal
:
Specifies the security context under which the task runs.New-ScheduledTaskTrigger
:
Determines when the task will execute.New-ScheduledTaskSettingsSet
:
Configures task settings such as allowing on-demand starts or delaying the task.Register-ScheduledTask
:
Creates and registers the task.
Building a scheduled task with PowerShell
Step 1: Define the action
The action specifies what the task will do. For example, to run a PowerShell script:
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "C:\Scripts\MyScript.ps1"
Step 2: Set the trigger
The trigger defines when the task should run. A common trigger is to run at logon:
$trigger = New-ScheduledTaskTrigger -AtLogon
You can also create a trigger that runs at a specific time:
$trigger = New-ScheduledTaskTrigger -Once -At 7am
If you want the task to repeat, add a repetition interval:
$trigger.Repetition = New-ScheduledTaskTrigger -RepetitionInterval (New-TimeSpan -Minutes 30)
Step 3: Specify the principal (run as)
The principal determines the security context the task runs under. To run as the system or another user:
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
If you need it to run as the logged-in user:
$principal = New-ScheduledTaskPrincipal -GroupId "Users"
Step 4: Set task settings
Configure task settings to fine-tune behavior:
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Step 5: Create and register the task
Combine the components and register the task:
Register-ScheduledTask -TaskName "MyUserProfileTask" -Action $action -Trigger $trigger -Principal $principal -Settings $settings
Why use a random delay?
In some cases, adding a random delay can prevent tasks from running simultaneously on multiple machines, which helps with load balancing. For example:
$trigger.Delay = (New-TimeSpan -Minutes (Get-Random -Minimum 5 -Maximum 15))
Real-world use cases
1. Removing per-user software
When software is installed per user (like Firefox or Appx packages), you may need to run a script to remove it from each profile. Creating a scheduled task that runs at logon ensures this happens without manual intervention.
Example PowerShell script:
# Define a whitelist of apps to keep
$whitelist = @("Microsoft.Paint", "Microsoft.Calculator")
# Get installed Appx packages
$appxPackages = Get-AppxPackage -AllUsers
# Remove unwanted packages
foreach ($app in $appxPackages) {
if ($app.Name -notin $whitelist) {
Remove-AppxPackage -Package $app.PackageFullName -AllUsers
}
}
2. Running custom logon tasks
If you need to apply configuration changes, map network drives, or perform other tasks when a user logs in, a scheduled task can handle these operations automatically.
Important considerations
Run level: Ensure that the task runs with the appropriate permissions, especially if modifying system settings.
Error handling: Consider adding logging or error handling to capture failures or unexpected behavior.
Testing: Always test tasks on a few machines before deploying them across your environment.
Conclusion
If you can use Group Policy, it’s the easiest and most reliable way to manage user-specific actions. But for environments where Group Policy isn’t available, PowerShell scheduled tasks provide a powerful alternative. By using well-defined actions, triggers, and principals, you can automate tasks and manage user environments effectively.
If you’re looking for an even simpler way to manage tasks, PDQ Deploy can streamline deployments and scheduled tasks across your organization. Try PDQ Deploy and Inventory free for 14 days!
Loading...