PowerShell not setting PSModulePath correctly for Windows "Local System" user

63 views Asked by At

I have a service that runs as the user SYSTEM which calls a PowerShell script. Up until about a month ago everything was working just fine. But after the last Windows update that I applied, things are no longer working. The error it gives is "ObjectNotFound" for simple things like Get-ExecutionPolicy and Get-IISSite. I instrumented the script and found out that before my last patch update the value for PSModulePath was

C:\Program
Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

while after patching it is

WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

Windows/PowerShell added the "WindowsPowerShell\Modules;" part to the beginning of the path, which I would expect for a normal user, except that it would be "C:\Users\username\Documents\WindowsPowerShell\Modules;" so it looks like Windows could find a value for %HOME% for the SYSTEM user and just added the rest of the path anyway.

Has anyone else seen this? Is there a configuration setting somewhere to tell PowerShell to not add a user's personal PowerShell modules directory to PSModulePath?

I manually set the value

$Env:PSModulePath = "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules"

removing the user portion, within the script and it works correctly after that, but I shouldn't have to do that. I'm sure there's a more correct way to address this.

0

There are 0 answers