Import-Module WebAdministration wont load from script but does from command line Import-Module WebAdministration wont load from script but does from command line powershell powershell

Import-Module WebAdministration wont load from script but does from command line


For servers you need to install the role Management Tools under Web Server (IIS) to use the WebAdministration module. To see if you have the module available use Get-Module -ListAvailable.

For Windows 7 to 10 you will need to install the feature IIS Management Scripts and tools under Internet Information Services >> Web Management Tools.

You could try manually locating the WebAdministration .psd1 file and then import it. Use $env:psmodulepath to help locate where your modules are stored then run:

Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1' 

If Server 2008 you could try the following but this may not work on 2012 and upwards:

Add-PSSnapin WebAdministration

Note You will need to run the script with administrator rights to be able to load the WebAdministration module with Import-Module or Add-PSSnapin.

Also check that you have PowerShell's execution Policy set to Unrestricted:

Set-ExecutionPolicy unrestricted

You might want to see this Question.


In the end there was a problem something, possibly chocolatey?, was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script.

I found it by logging $env:PSModulePath at different points in the scripts that I was running.

I worked around it by reordering the entries in $env:PSModulePath.

Have a look at @Richard's answer for some other good suggestions.


I had the same situation, i've fixed it installing the Windows Feature Web-Scripting-Tools on W2016 Server:

Add-WindowsFeature Web-Scripting-Tools