Invoke-AsCurrentUser_WithArgs

1.1.1

Execute PowerShell scriptblocks in the context of the currently logged-in user from a SYSTEM context. Ideal for Intune deployments, SCCM task sequences, and scheduled tasks. Supports argument passing, transcript capture, stream capture, and configurable timeouts.

Minimum PowerShell version

5.1

Installation Options

Copy and Paste the following command to install this package using PowerShellGet More Info

Install-Module -Name Invoke-AsCurrentUser_WithArgs

Copy and Paste the following command to install this package using Microsoft.PowerShell.PSResourceGet More Info

Install-PSResource -Name Invoke-AsCurrentUser_WithArgs

You can deploy this package directly to Azure Automation. Note that deploying packages with dependencies will deploy all the dependencies to Azure Automation. Learn More

Manually download the .nupkg file to your system's default download location. Note that the file won't be unpacked, and won't include any dependencies. Learn More

Owners

Copyright

(c) 2025-2026. All rights reserved.

Package Details

Author(s)

  • Harze2k

Tags

RunAsUser Intune SCCM SYSTEM UserContext Deployment Windows

Functions

Invoke-AsCurrentUser_WithArgs Serialize-Object Deserialize-Object Get-RunAsUserCSharpSource

PSEditions

Desktop Core

Dependencies

This module has no dependencies.

Release Notes

## Version 1.1.1
- FIXED: Get-SafeTempPath now prefers ProgramData\UserInvoke\Temp and explicitly grants Authenticated Users (Modify) on the exchange directory. Previously C:\Windows\Temp\UserInvoke was selected and write-tested in the CALLER's context (SYSTEM), but the helper runs as the TARGET USER who cannot access C:\Windows\Temp - every waiting call from SYSTEM to a standard user (or with -NonElevatedSession) failed with 'Timeout waiting for result file' because the helper could neither read data.json nor write result.json.
- NEW: -SessionTokenRetrySeconds parameter (default 15, 0 = previous fail-fast behavior). WTSQueryUserToken is retried within this window when no interactive session token exists yet (Win32 error 1008), which happens when the caller fires on a logon trigger seconds before winlogon publishes the token.
- IMPROVED: the result-file wait fails fast with an explanatory message when the helper process has exited without reporting back, instead of burning the full timeout and masking the cause.
- IMPROVED: exactly ONE catchable error per failure. Inner layers now Write-Warning and return structured results; previously their Write-Error calls were promoted to terminating errors under callers using -ErrorAction Stop, which skipped the documented structured return and produced 4-5 duplicate transcript entries per failure. Note: failures are now reported on the error stream even with -Quiet (-Quiet suppresses output, not failures).

## Version 1.1.0
- Replaced all throw statements with Write-Error and graceful return patterns to prevent script termination
- Improved error handling: functions now return $null on failure instead of throwing
- Added consistent ExecutionSuccess property to all result output paths
- Stream capture results (StdOut, StdErr, Warnings, Verbose) now always present when -CaptureStreams is used (empty string instead of missing)
- Improved parameter validation error messages with Write-Error instead of silent Write-Warning
- Updated function descriptions and synopsis for clarity
- Fixed WorkingDirectory parameter validation to handle empty strings properly
- Added error result output on Add-Type compilation failure
- Improved timeout handling to return structured error objects instead of throwing
- Backup log file naming now uses timestamp instead of random number for easier troubleshooting

## Version 1.0.0
- Initial release
- Execute scriptblocks as currently logged-in user
- Pass arguments/variables to scriptblocks
- Capture transcript and execution results
- Support for PowerShell 5.1 and 7+
- Configurable timeout handling
- Optional stream capture (stdout/stderr)

FileList

Version History

Version Downloads Last updated
1.1.1 (current version) 91 6/11/2026
1.1.0 118 3/13/2026
1.0.0 809 12/1/2025