liveBook · Manning

Invoke-CommandAs

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

Invoke-CommandAs is a PowerShell module that runs commands or script blocks under specified Windows security contexts, including the system account.

Invoke-CommandAs is a PowerShell module for testing commands and script blocks under Windows security contexts, including the system account. System execution differs from elevation: it cannot access the interactive user's profile or registry hive. section 11.4.2

Use it when automation must distinguish user-scoped and machine-scoped effects:

  • Install the module in an elevated Windows PowerShell 5.1 prompt, import it, and invoke a script with -AsSystem. section 11.4.2
  • Expect system execution to write machine resources while leaving user settings such as the default Git branch unchanged. section 11.4.2
  • Refresh environment variables or reboot after installation before validating commands in a new PowerShell session. section 11.4.2

Run an installer as SYSTEM

Install-Module -Name Invoke-CommandAs Import-Module -Name Invoke-CommandAs Invoke-CommandAs -ScriptBlock { . C:\git-install.ps1 } -AsSystem

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Distinguishes SYSTEM from elevation, demonstrates invocation, and stops short of module internals.
Examples
Demonstrates installing Git through a script block and checking system-scoped Git configuration afterward.
What it assumes
Assumes PowerShell familiarity and an elevated prompt; teaches the distinction between administrator and SYSTEM contexts.
What you can do after
Operate Invoke-CommandAs to run and validate Windows installation scripts as SYSTEM.
How it connects
Connects PowerShell automation with Windows security contexts, user profiles, registry hives, PSExec, and Active Setup.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.