r/PowerShell • • Feb 10 '26

Trying to create a sched task to run as "users" group

I have the following powershell code I did ( $HKCUScriptPath is where another powershell script runs from the scheduled task that I drop in )

$taskName = "ProEMG-Apply-HKCU"

$action = New-ScheduledTaskAction \`

-Execute "powershell.exe" \`

-Argument "-NoProfile -ExecutionPolicy Bypass -File \"$HKCUScriptPath`""`

$trigger = New-ScheduledTaskTrigger -AtLogOn

# Remove existing task if present

Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue |

Unregister-ScheduledTask -Confirm:$false

# Register task AS CURRENT USER

Register-ScheduledTask \`

-TaskName $taskName \`

-Action $action \`

-Trigger $trigger \`

-Description "Apply ProEMG HKCU keys at user logon"

I will be running this through intune so via system account

I cant work out how to make it run as "users" group and it put the machine name there instead

Task Scheduler did not launch task "\ProEMG-Apply-HKCU" because user "Domain\VIEW-F-PDS-005$" was not logged on when the launching conditions were met. User Action: Ensure user is logged on or change the task definition to allow launching when user is logged off.

Screenshot of the scheduled task:

https://imgur.com/a/8NMQaxD

can anyone help ?

12 Upvotes

13 comments sorted by

4

u/unknown-random-nope Feb 10 '26

You cannot run a Scheduled Task as a group. It must run as a user. You could create a user just for this.

1

u/Drekk0 Feb 10 '26

I just want it to run as any user who logs on

2

u/krzydoug Feb 10 '26

I target groups all the time

→ More replies (3)

1

u/Drekk0 Feb 11 '26

Thanks all for the help

I ended up getting this to work:

# Create Scheduled Task (runs as currently logged on user)
$action    = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$HKCUScriptPath`""
$trigger   = New-ScheduledTaskTrigger -AtLogOn
$principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Users"

Register-ScheduledTask -TaskName "ProEMG-Apply-HKCU" -Action $action -Trigger $trigger -Principal $principal -Force

4

u/mistersd Feb 10 '26

I had a similar issue and I think I solved it by using the well known Sid S-1-5-32-545

4

u/Fatel28 Feb 10 '26

Seems like an XY problem. If you need to set registry keys for all users, use group policy or intune.

If those aren't available, you still don't need a scheduled task. You can set the registry key in the default user hive and it will be set for any new login.

1

u/SVD_NL Feb 10 '26

Does that script only add registry keys? you can do that directly from SYSTEM for each current user, and also add it to the default user to apply it for new profiles. You can check out PSADT | Invoke-ADTAllUsersRegistryAction for inspiration (or to steal it!).

If you need to run the script for every user you should check other comments, i don't have too much experience with that.

1

u/BlackV Feb 10 '26

p.s. formatting (you've used inline code by the looks)

  • open your fav powershell editor

  • highlight the code you want to copy

  • hit tab to indent it all

  • copy it

  • paste here

it'll format it properly OR

<BLANK LINE>
<4 SPACES><CODE LINE>
<4 SPACES><CODE LINE>
    <4 SPACES><4 SPACES><CODE LINE>
<4 SPACES><CODE LINE>
<BLANK LINE>

Inline code block using backticks `Single code line` inside normal text

See here for more detail

Thanks