Powershell: Can MSI used to Install PowerShell Core on Windows Create CurrentUser Module Paths?

Created on 2 Mar 2019  路  6Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

As a user installing PowerShell Core on Windows via the downloadable MSI file, I would like all the directories referenced in $env:PSModulePath to actually exist on my system following installation of PowerShell Core, so that PowerShellGet cmdlets like Install-Module -Scope CurrentUser work 'out of the box' without needing to manually create required directory paths.

Proposed technical implementation details

Straight after installation and launching PowerShell 6.1.3 Core, it would be nice to be able to run:

PS C:\Users\Me> Install-Module -Name Az -Scope CurrentUser

.. without receiving the Error:

PackageManagement\Install-Package : Could not find a part of the path 'C:\Users\Me\Documents\PowerShell\Modules\Az.Accounts\1.3.0'.
At C:\program files\powershell\6\Modules\PowerShellGet\PSModule.psm1:9491 char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

As the Module Paths for PowerShell Core differ from those used in PowerShell for Windows 5.1 (also installed), I'm assuming I need to manually create the 'C:\Users\Me\Documents\PowerShell\Modules' path for Install-Package to work. However, I think it would be a smoother experience if a path being referenced in $env:PSModulePath (and critical for PowerShellGet) exists after installation of PowerShell.

The MSI File is available at the following URL, which contains no instructions for manually creating directories like 'C:\Users\Me\Documents\PowerShell\Modules' or 'C:\Program Files\PowerShell\Modules':

https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-6

Issue-Question Resolution-Answered

Most helpful comment

Even though this was not a Powershell issue, but a Windows Defender issue, I'm reopening temporarily in case anyone else encounters a similar issue using Install-Module. This was a little tricky to track down.

The problem was a recent update to Windows Defender, which has a Ransomware Protection feature that automatically includes profile folders (C:\Users\<yourname>\Documents etc) as "Protected Folders". Applications such as cmd.exe and Powershell Core (pwsh.exe) have to be manually added under "Allow an app through Controlled folder access". There seems to be no way to exclude a subfolder such as PowerShell\Modules under a Protected Folder that's been added (and I'm thinking it would not be smart idea to remove C:\Users\<yourname>\Documents being a Malware-protected folder)

Does this mean Powershell needs to reconsider the Module location being under "MyDocuments" for the local user under Windows? Perhaps the MSI file should add an exception for the Module location as part of the setup?

Feel free to close, as I should likely create a new Issue based on this new information. Just wanted to make this visible in case anyone else encounters this.

All 6 comments

PS C:\Users\Me> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Closing after I found that even "mkdir" from a Command-Prompt has similar behavior ('Cannot find the file specified'), so this is likely not related to PowerShell (strangely, creating Folders from Explorer works)
Looks like I have bigger problems....
Will close and re-open if necessary once my system is stable.

Even though this was not a Powershell issue, but a Windows Defender issue, I'm reopening temporarily in case anyone else encounters a similar issue using Install-Module. This was a little tricky to track down.

The problem was a recent update to Windows Defender, which has a Ransomware Protection feature that automatically includes profile folders (C:\Users\<yourname>\Documents etc) as "Protected Folders". Applications such as cmd.exe and Powershell Core (pwsh.exe) have to be manually added under "Allow an app through Controlled folder access". There seems to be no way to exclude a subfolder such as PowerShell\Modules under a Protected Folder that's been added (and I'm thinking it would not be smart idea to remove C:\Users\<yourname>\Documents being a Malware-protected folder)

Does this mean Powershell needs to reconsider the Module location being under "MyDocuments" for the local user under Windows? Perhaps the MSI file should add an exception for the Module location as part of the setup?

Feel free to close, as I should likely create a new Issue based on this new information. Just wanted to make this visible in case anyone else encounters this.

I think leaving this issue with the context is valuable. 馃檪

It looks like the available options are, essentially:

  1. Add an exception in Defender for the pwsh modules location.
  2. Move the modules location.

I'd guess option 1 will likely be preferred, but that depends how difficult it is to create an exception during the install.

It is not first report for the problem. Search works so close the issue.

Thank YOU very much! Could not find anything about solving the "Could not find path" error anywhere. Allowing Powershell in Windows Defender did the trick.

Was this page helpful?
0 / 5 - 0 ratings