Powershell Command was found but the module could not be loaded
In one of my most recent live coding sessions, I had an issue with my PowerShell configuration. When running any of the Azure PowerShell cmdlets I got the message
The ‘
[command]
’ command was found in the module ‘Az.[someModule]
’, but the module could not be loaded. For more information, run the ‘Import-Module Az.someModule
’
Running the Import-Module
command didn’t help much, because the module was already loaded. I even tried Import-Module Az
and verified everything was installed correctly with the Get-InstalledModule -Name Az*
command. This all appeared to be correct.
After my session, I researched the matter a bit more and read the Stack Overflow post with the title ‘Import powershell module fails’. This one pointed me in the right direction.
After printing the $env:PSModulePath
I saw at least one of the folders was pointing to my Documents folder, which has its location redirected to OneDrive.
Nowadays, OneDrive has a nice feature where files aren’t downloaded automatically, but you can choose which files/folders to sync on your machine. The WindowsPowerShell
folder wasn’t synced, yet.
This can be solved by selecting the Always keep on this device
option in OneDrive
Once all files & modules are loaded, you can start working with Azure via PowerShell again.
I had to make sure both my PowerShell
and WindowsPowerShell
were synced on my device as both are used, depending on which version of PowerShell you are using.
Note
If you don’t see the option ‘Always keep on this device’ or any other OneDrive option in the context menu, there’s a chance you’re running as an Administrator. To get the context menu, you need to turn on the UAC (via the UI or Group Policy) and reboot the machine. That’s what did the trick at my machine in the cloud.