About PowerShell Module Paths
This page relates my battle with OneDrive folder backup and how I fixed my broken
When trying to use ConditionalAccessAsCode I ran into a problem with running one of it's dependent modules which is the AzureADPreview module (which is only compatible with PowerShell 5.1 and not PowerShell Core (i.e. versions 6 and above))
Install-Module -Name AzureADPreview in PowerShell 5.1 I was getting an error running
Import-Module -Name AzureADPreview:
The specified module was not loaded because no valid module file was found in any module directory
Which seemed very strange given that it seemed to have installed without error.
I worked out that with
Install-Module the module was being installed into:
Import-Module was trying to load modules from:
I quickly realised that this was because I had enabled the personal folder backup feature in OneDrive and that it had changed the path of my
As I subsequently discovered, it does state in this document on that:
Microsoft OneDrive can also change the location of your Documents folder. You can verify the location of your Documents folder using the following command: [Environment]::GetFolderPath('MyDocuments').
So the cause of my problem was that the path of my
MyDocuments folder had been updated form
C:\Users\rohan\OneDrive\Documents (after enabling OneDrive PC folder backup), but that the entry for
PSModulePath in my [User] Environment Variable (in PowerShell
$env:PSModulePath) had not been updated accordingly.
One solution available to me was to update the value of
PSModulePath in my [User] Environment Variable to add the
\OneDrive\ segment into the entry for the
Disable OneDrive folder backup¶
If I disable backup then the path can go back to normal and will resolve the issue with
$env:PSModulePath without having to edit the Environment Variable.
See the section 'Manage or stop PC folder backup' in this article
As it states:
When you stop backing up a folder, the files that were already backed up by OneDrive stay in the OneDrive folder, and will no longer appear in your device folder.
And so I also then need to move the
WindowsPowershell folders back into the MyDocuments folder
Change the location of OneDrive folder¶
I'll note this here too as I didn't do this myself and it still won't prevent the issue if you elect to use OneDrive personal folder backup...
A demo of PSModulePath in action¶
PowerShell 5 and PowerShell Core download modules into different paths and so I've prepared a couple of demos below to illustrate this.
The full script for the demo is:
"Running in Powershell $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" $env:PSModulePath Install-Module -Name PSLogging -Scope CurrentUser Get-InstalledModule Get-InstalledModule -Name PSLogging Get-Module -Name PSLogging Import-Module -Name PSLogging Get-Module -Name PSLogging (Get-Module -Name PSLogging).Path Remove-Module -Name PSLogging Uninstall-Module -Name PSLogging
"Running in Powershell $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" $env:PSModulePath # Note: Installing sudo to avoid having to open an elevated shell scoop install sudo sudo Install-Module -Name PSLogging -Scope AllUsers Get-InstalledModule Get-InstalledModule -Name PSLogging Get-Module -Name PSLogging Import-Module -Name PSLogging Get-Module -Name PSLogging (Get-Module -Name PSLogging).Path Remove-Module -Name PSLogging sudo Uninstall-Module -Name PSLogging