Powershell shows PSModulePath is doubled up

1

Here is my problem:

PS C:\windows\system32> $env:PSModulePath.Replace(';',"`n")
C:\Users\sirdank\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\windows\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files (x86)\Microsoft SQLServer\120\Tools\PowerShell\Modules\
C:\Program Files(x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\
C:\Users\sirdank\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\windows\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files (x86)\Microsoft SQLServer\120\Tools\PowerShell\Modules\
C:\Program Files(x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\
C:\ProgramFiles (x86)\Microsoft SDKs\Azure\PowerShell\Storage\

This happened after I ran Get-Module xWebAdministration because I want to use Azure Desired State Configuration. Since then, I've tried

  • Modifying PSModulePath through control panel
  • Modifying it using SETX
  • Modifying it using [Environment]::SetEnvironmentVariable()
  • Checking all six powershell profiles to see if they're modifying it (none of them even exist)
  • Rebooting my laptop twice

None of these things has fixed my problem. When I look in control panel, PSModulePath appears to be set correctly. However, when I open powershell, it still displays the output above and DSC still doesn't work because it tries to get xWebAdministration twice. How do I fix my PSModulePath?

sirdank

Posted 2016-11-30T16:26:07.247

Reputation: 203

Answers

0

I've got powershell working again but I had to create $Home\Documents\Profile.ps1 with the contents

$env:PSModulePath = [string]::join(';', ($env:PSModulePath -Split ';' | % {$_.TrimEnd('\')} | Sort-Object -Unique))

sirdank

Posted 2016-11-30T16:26:07.247

Reputation: 203

0

If PSModulePath is set as both a user variable and a system variable, the two will be combined, like PATH is. Bizarrely, only the user variable will show up in the command prompt if you do echo %PSModulePath%, but $env:PSModulePath in PowerShell shows the real combined value, as does the original batch command if you launch cmd from PowerShell. It appears that the Modules folders descended from your Documents and from Program Files are added by PowerShell at runtime, so they needn't be provided by environment variables.

To deduplicate the module paths, remove the extra environment variable or edit it to not repeat any folders.

Ben N

Posted 2016-11-30T16:26:07.247

Reputation: 32 973