David Kehoe
David Kehoe

Reputation: 23

Powershell Closes Instantly

I've looked all of the the internet and I cannot find any information that applies to this situation.

Powershell closes immediately upon starting. When I run it in Command Prompt I get the following:

Windows PowerShell terminated with the following error:
 Unable to cast object of type 'System.String' to type 'System.String[]'.

I have checked the following folders and no profiles exist:

c:\users\me\appdata\microsoft\windows\powershell

c:\windows\system32\windowspowershell\v1.0\

c:\windows\systwow64\windowspowershell\v1.0\

I have tried to run with the following commands and have no luck:

powershell -noexit

powershell -noprofile

I have run the following commands and have no luck:

sfc.exe /scannow

DISM.exe /Online /Cleanup-image /Restorehealth

I also have gone to Control Panel -> Uninstall a Program -> Turn Windows features on or off then, Removed Powershell, rebooted, then re-installed it.

After doing all of these steps I still am not able to run Powershell. ISE does not work either.

Upvotes: 2

Views: 4597

Answers (2)

user15478373
user15478373

Reputation: 11

This may be related to Powershell logging settings. I had the exact same issue after implementing Powershell Module logging using the wrong path for ModuleNames.

Check the values set in HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging

In my case, I made the mistake of adding a value of ModuleNames set to * - not realizing it should be a KEY named ModuleNames - which resulted in an error

Windows PowerShell terminated with the following error:
Unable to cast object of type 'System.String' to type 'System.String[]'.

By adding the right path HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\ with a REG_SZ value named * and with a value of * I got Powershell working again.

Registry image

Note that if this setting comes from a GPO (Group Policy Object) in the domain, it will need to be fixed there, rather than in the registry. Otherwise, the GPO will just overwrite the local settings the next time it applies.

Upvotes: 1

Jawad
Jawad

Reputation: 11364

Whenever you run powershell, it loads the default modules present in the Modules directory. One of these modules (most likely a custom one you wrote) is causing errors and not allowing you to start.

Without knowing anything about the modules you have present in the directory located at: C:\windows\system32\windowspowershell\v1.0\Modules, it would be hard for anyone to tell you the solution.

Recommendation

Remove any custom modules you have in there and add each module you need one at a time to see which one breaks your powershell.exe. You will need to check each path you have defined for custom modules to be loaded as well.

Other way would be to clear out the PSModulePath from Environment variables and add one location at a time until you see which Modules directory is causing error.

NOTE: Write down the paths on a notepad somewhere before you clear it.

From the error it seems like a .net library class (dll) that is not correctly written.

Upvotes: 0

Related Questions