Powershell issues running script from Windows Service 01/02 - powershell modules not available

Occasional Contributor


I am facing problems executing some powershell scripts from a Windows Service.



The Windows Service (started with an AD user) will execute a command through Process Class.

It will launch a command like in the folder that we specify:

  • powershell .\<script name>.ps1 -param1 'param1' -param2 'param2'...



Some of the powershell modules are not "available" to be imported, although they are available in the folder "C:\Program Files\WindowsPowerShell\Modules". E.g.:

  • Import-Module -Name Az
  • Import-Module -Name SqlServer

If we run the same command from a Command Prompt the script executes correctly!



Run the following command:

  • robocopy "C:\Program Files\WindowsPowerShell\Modules" %USERPROFILE%\Documents\WindowsPowerShell\Modules /mir

Thanks in advance,

Tiago R.



3 Replies


Is this user running this service is an admin, maybe this user doesn't have the proper permission to load the module from that directory, 

Try to include the Import-Module XXXXX in your script and execute it.

what is the output? are you getting any errors when Import-module is executed?

The user running the service is an administrator in the server.
If I connect to the server thru Remote Desktop and execute the command in a Command Prompt the script executes successfully.
I think the problem is that executing the command from the service's scope the complete profile of the user is not loaded. Is there a way to force loading the full profile of the user before running the script?
best response confirmed by tarente (Occasional Contributor)
The problem was that powershell was executing in 32-bits thru the service and in 64-bits when launched from the command prompt.
I copied the code in described in to the beginning of the script and now works as expected.