0

TLDR: 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. Created a temporary user and repeated all the same steps I did on the main user (again, brand new machine) and it can function just fine.

MAIN POST:

Suddenly my powershell broke. Commands such as Write-Host or Get-ChildItems do not work:

PS C:\Windows\System32\WindowsPowerShell\v1.0> write-host 'test'
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 'test'
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (write-host:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I did a round of troubleshooting, uninstalled some recently installed OR updated software, and it seemed to start working again, but after couple of hours it begum having the same problems. Any ideas how to troubleshoot?

Update: Troubleshooting further. Ran (Get-Command -type cmdlet Write-Host).ModuleName on a healthy computer, got Microsoft.PowerShell.Utility Ran Get-Module "Microsoft.PowerShell.Utility" on bad computer - no output, meaning module is not loaded. because on healthy computer it did show some output for that module. Why is not this module imported? If I say Import-Module Microsoft.PowerShell.Utility on bad computer, Write-Host starts working again. Why is it not autoloading as always?

Update2: As asked in the comments, output of $PSVersionTable is:

Name                           Value
----                           -----
PSVersion                      5.1.18362.1171
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1171
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Basically, this explains the overall problem:

PS C:\Users\myUser> Write-Host 'hello'
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 'hello'
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\myUser> Import-Module Microsoft.PowerShell.Utility
PS C:\Users\myUser> Write-Host 'hello'
hello
PS C:\Users\myUser>

Received a new machine today and bumped into exactly the same problem. Basically, what happened: OldMachine: Basic powershell 5.1 commands stopped working. Troubleshooting showed that almost all the modules would not autoload, so I could still load them manually. Not all of them though - some had very elaborate dependancies.

  • Checked all the environmental variables - they were OK.
  • Did windows restore - did not help.
  • Created a new local user, everything works under new user.

I wasn't able to fix the issue so I got a replacement computer. Brand new. Started setting it up and only did 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

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
 

So I think it happened when I was installing this updated package manager. Any ideas?

Update: 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. The only difference between two users that I can detect, my test user has folder "C:\Users\testuser\AppData\Local\PackageManagement\ProviderAssemblies" with nuget installed in it. My main user doesn't have this folder. How can I install it NOT from powershell? Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force doesn't work as pretty much anything in powershell, and when I try to do Import-Module PackageManagement it complains that it doesn't know Join-Path and so on...

Update2: I did go down that rabbit hole and looks like imported PackageManagement Found the following folder: "C:\Users\testuser\Documents\WindowsPowerShell\Modules\PackageManagement\1.4.7" having folders "coreclr" and "fullclr" that were missing in my main user. Copying them helped install the PackageManagement. Still doesn't solve the problem.

Update3: So, when I start a fresh instance of PowerShell and type Write-Host, I get 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
 

which is strange since the module that has this cmdlet is located in C:\Windows\System32\WindowsPowerShell\v1.0\Modules and should be loaded automatically. However, if I copy subfolder Microsoft.PowerShell.Utility into other folder where modules are searched for: C:\Users\myMainUser\Documents\WindowsPowerShell\Modules - everything starts working. So, what's so special about C:\Windows\System32\WindowsPowerShell\v1.0\Modules? it IS listed in $ENV:PSModulePath:

PS C:\WINDOWS\system32> $ENV:PSModulePath
C:\Users\myMainUser\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\Window
7
  • 1
    PowerShell 7 by default isn’t installed to C:\Windows\System32\WindowsPowerShell\v1.0, it appears you are opening a legacy PowerShell console instead of a PowerShell 7 console. Can you provide a screenshot of your start menu search results for PowerShell? Please do not submit a comment instead edit your question. Also provide the output of $PSVersionTable
    – Ramhound
    Dec 30, 2020 at 1:23
  • The problem is not with Powershell 7, it's working perfectly and I uninstalled it (I need to develop on Powershell 5.1 since it's default for Windows). The problem is with PowerShell 5.1 which at some point stopped autoloading basic modules.
    – miguello
    Dec 30, 2020 at 1:30
  • That most definitely is not clear by your question. You should note you should probably target PowerShell 7 since that’s the future. You are aware that 1903 support ended on December 9 2020? If that’s intentional your build (1171) also is several months old. This probably mean your missing a .NET patch, which might have nothing to do with anything, but could be significant
    – Ramhound
    Dec 30, 2020 at 1:37
  • PowerShell 5.1 is the last version of PowerShell 5.x, the next version, is PowerShell Core 6. PowerShell 7 is the current version, replaces both PowerShell 5.1 and PowerShell Core 6. 18362.1171 is 1903, not 1909. However, it doesn't matter, on 1909 PowerShell is suppose to be 18362 (after looking at my own working machine).
    – Ramhound
    Dec 30, 2020 at 1:55
  • 1
    @giacomo1968 thanks, and I updated this one with details from the new one.
    – miguello
    Jan 5, 2021 at 3:59

2 Answers 2

1

I had a similar issue:

The 'new-object' command was found in the module 'Microsoft.PowerShell.Utility', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Utility'.

When running 'Import-Module Microsoft.PowerShell.Utility', I got errors referencing:

Microsoft.PowerShell.Utility module version=3.0.0.0 but on another identical OS+PowerShell host, it should be 3.1.0.0

Maybe an update went bad? So I ran the DISM+SFC+Chkdsk+Reboot gamut, no luck. Reinstall/Repair .NET, no change. PSModule paths, etc. etc. no fix.

Digging further, read up on modules and decided to open

"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1":

@{ GUID="1DA87E53-152B-403E-98DC-74D7B4D63D59" Author="Microsoft Corporation" CompanyName="Microsoft Corporation" Copyright="© Microsoft Corporation. All rights reserved." ModuleVersion="3.1.0.0" PowerShellVersion = '5.1'

...

NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

Mine had different GUID and ModuleVersion=3.0.0.0

I found Microsoft.PowerShell.Commands.Utility.dll located here:

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll

Right-click>Properties>Details showed most of them blank. I assumed the file was corrupted.

What fixed it for me:

Zipped "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Utility folder C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll from an identical working system, restored to same location after renaming originals

Executed the answer by miguello above to re-import all modules, then successfully installed NuGet, and after a reboot, powershell was working again!

Thanks!

0

This fixed it for me:

(Get-Module -ListAvailable).Name | %{Import-Module -Name $_ -Force}
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-Module -Name PackageManagement -Force -RequiredVersion 1.4.6 -Scope AllUsers -AllowClobber -Verbose

Then if the last command doesn't install NuGet do this:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .