question

MikhailLadanov-1112 avatar image
0 Votes"
MikhailLadanov-1112 asked RoadrunnerLI commented

Powershell broke - basic commands not working, not loading modules from common folders

At some point my Powershell broke. Standard commands like 'Write-Host' stopped working.
I was using VS Code on Windows 10 and using PowerShell 5.1
Checked all the environmental variables, everything looks fine.
Couldn't fix it and got a new machine.
Did literally the following:
- Installed VS Code
- Turned on Powershell extension in VS Code
- It prompted something about Package Manager needing an update or it will not work properly
- Asked me to install NuGet to get the Package Manager
- After that PowerShell 5.1 stopped working again.
- Somewhere in between installed Git

After that PowerShell stopped working.
Exactly the same symptoms: Modules not autoloading, fresh instance of powershell does this:

 PS C:\WINDOWS\system32> Write-Host
 Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify 
 that the path is correct and try again.
 At line:1 char:1
 + Write-Host
 + ~~~~~~~~~~
     + CategoryInfo          : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
     + FullyQualifiedErrorId : CommandNotFoundException

Did some more troubleshooting. Created a local user and repeated all the steps:
- Created a local user
- Installed VS Code
- Installed Powershell Extension within VS Code
- It asked to update PackageManagement
- Said it needed NuGet to update PackageManagement, installed
- Installed Git

Everything is working.
Some more troubleshooting showed that my main user does not look into folders C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules when looking for a cmdlet. It only looks into C:\Users\myMainUser\Documents\WindowsPowerShell\Modules. So I either have to do Import-Module -Name Microsoft.PowerShell.Management and so on, or copy all the modules into C:\Users\myMainUser\Documents\WindowsPowerShell\Modules.
My temporary user apparently functions normally.
Any ideas?

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I know it's quite a while ago and you may have fixed the problem alredy somehow. But in case that someone else is facing the same issue, here is what we found.
We had the same effect: Remote or directly on the client, most of the common PowerShell commands like 'Get-Host' were "...not recognized as the name of a cmdlet". The reason for this was some basic PS modules were not automatically imported on startup (e.g. Microsoft.PowerShell.Management).
Running the command "Get-Module -ListAvailable -Refresh" should usually correct such issues when they are caused by soem kind of cache corruption, but when I run it, I saw that some modules that are present in "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\" were not even listed in the output of this command.
It turned out that a not yet identified process has change the attributes on some of the PS-Module-folders under the above path to "Hidden". That caused them to not load automatically on PS startup. Switching all folders attribute to visible fixed the problem immediately.

0 Votes 0 ·

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT commented

Hi,

Check if $env:PSModulePath contains C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules. If not, add them to the system variable "PSModulePath" in Environment Variables in System Properties window.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, it does contain all those paths. As I mentioned, "Checked all the environmental variables, everything looks fine."
$ENV:PSModulePath is identical between two users (except for the user name)

0 Votes 0 ·

What does $PSModuleAutoloadingPreference return? Set $PSModuleAutoLoadingPreference = 'All' and see if the modules are imported automatically.

0 Votes 0 ·

It was set to 'All'. Again, all variables are correct.

0 Votes 0 ·
Show more comments