After many hours of research and troubleshooting, I found that OneDrive was the culprit of my Powershell pain.
This is all in part to Microsoft’s default of putting modules (that are installed under the User scope) within your \Documents\WindowsPowerShell\Modules folder. Now, add the complexity that the native folder direction to the cloud adds (aka, Backup up folders on this PC).
By default, when you use the install-module cmdlet it installs via the user context, which is naturally more secure than having broader system permissions. But when you have multiple machines and multiple versions of modules, now you have a nightmare on all your devices.
I had to spend HOURS removing all of the modules that were installed via Powershell from each of my machines that I had synced with OneDrive, and the Graph ones were the WORST because they would see dependencies, some of which were other modules that were already removed (so I had to remove the folders manually).
I found for the Microsoft.Graph modules this one was handy to let it sit and run for, oh, a few hours and then fix the rest.
Get-Module -ListAvailable | ?{$_.name -like “*graph*”} | uninstall-module
It was painfully slow because of the constant background processing of OneDrive, which was ALSO causing my Powershell to run dog slow.
Contents
How’ve I’ve changed my behaviors
Operationally I’ve adopted the following processes
1. Make sure you install modules using AllUsers scope
install-module Microsoft.PowerShell.PSResourceGet -Scope AllUsers
This places any new modules in C:\Program Files\WindowsPowerShell\Modules
2. Change your Powershell profile working path outside of the hands of OneDrive
$profile = “C:\Users\cblackburn\PowerShell\Microsoft.PowerShell_profile.ps1”
Reference: https://blog.matrixpost.net/set-powershells-default-working-directory/
3. Set environment variables to the same path in #2 where your profile is located
$env:PSModulePath = $env:PSModulePath -replace “\\OneDrive – ConnectUC\\Documents\\WindowsPowerShell\\“,”\powershell\”
Reference: https://github.com/PowerShell/PowerShell/issues/8069#issuecomment-557545121
Uh oh
I did have an error that I couldn’t get around when clearing some modules
In my instance i couldn’t see the folder in the any longer in OneDrive and couldn’t delete the folder
After a quick search I found the answer was to pause Onedrive, go out to the Web version, and delete the folder there, then reenable sync and wait for the folder to be removed by the OneDrive client.
What did I learn??
1. While OneDrive folder backup is a GOOD thing, it’s definitely NOT good for your modules.
Even in a multi-device scenario, the constant change they invoke causes pain. And I’d much rather have to reinstall modules on machines than deal with their sync issues.
2. ALWAYS install Powershell modules using -Scope AllUsers.
And do this on each machine when you install modules, even if that means you’re installing Microsoft.Graph 5 times
Now I can focus my time on the finer things in life