Hi Adi Wijaya,
Yes, I've run into this a few times. It's usually not PowerShell itself. More often it comes down to how Task Scheduler sets up the security context for the task.
I'd start with the task's Principal settings: which account it's set to run as, the Logon Type, and whether "Run with highest privileges" is checked. Together these decide what kind of token the task gets when it starts, so a small difference there can change who the script actually runs as.
To see what's really happening, add a line at the top of the script that logs the current identity (for example whoami /all or [System.Security.Principal.WindowsIdentity]::GetCurrent()) to a file. Then compare that to the account configured in the task. The Task Scheduler Operational log in Event Viewer (Applications and Services Logs > Microsoft > Windows > TaskScheduler > Operational) is also worth checking. It often shows how the task was launched and which logon type was used. And if the task runs under a service account, a gMSA, or some kind of delegated credential, the effective context may not match what you'd expect at first glance.
A couple of other things to check:
- Does the script start any child processes? Those can end up with a different token or run under a different account.
- Did this start after a GPO or permission change? If so, export the XML of this task and of a similar task that still works, then compare them side by side. The differences usually show up quickly.
In my experience, the cause usually turns out to be a mismatch between the configured principal and the logon type actually used at runtime. If you check the principal settings, logon type, elevation, and the identity the script logs, you should be able to find where it goes wrong.
Hope this helps. If it does, please mark it as the accepted answer so others with the same issue can find it.
Jason