Windows PowerShell Launching and Closing abruptly

When you open Windows PowerShell, its window may appear briefly and close before you can read the error. Sometimes, PowerShell ISE also fails to start. In this guide, I will show you how to troubleshoot Windows PowerShell closing immediately, identify startup errors, and repair damaged Windows components.

These steps focus on Windows PowerShell 5.1, which uses powershell.exe. PowerShell 7 uses pwsh.exe and installs alongside Windows PowerShell. Installing PowerShell 7 does not repair the Windows PowerShell installation.

Why Does Windows PowerShell Close Immediately?

When attempting to launch PowerShell on a Windows 11 device, I encountered an issue where the PowerShell window flashed briefly and then closed automatically. PowerShell ISE also failed to launch on the device. Even after restarting the device multiple times, the issue remained unresolved. I also tried launching PowerShell from Command Prompt, but it failed with the following error:

Windows PowerShell terminated with the following error: Could not load file or assembly ‘System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The module was expected to contain an assembly manifest.

Could not load file or assembly

Root Cause

If you encounter this error, the issue may arise from a corrupted system file or module called System.Management.Automation.dll. This file is typically located at C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35.

Solution

1. Take Ownership of the System.Management.Automation.dll

takeown /f C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
Take Ownership of System.Management.Automation.dll file

2. Grant Administrators full control of the System.Management.Automation.dll

icacls "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" /GRANT ADMINISTRATORS:F
icacls "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" /GRANT ADMINISTRATORS:F

3. Copy System.Management.Automation.dll from a Working PC

  • Sign in to another Windows 10 or Windows 11 device where PowerShell is working correctly.
  • Go to C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\ and copy the System.Management.Automation.dll file.
  • Return to the affected device where PowerShell is not working.
  • Paste the copied file to a temporary location on the affected device, for example, C:\Temp.
  • Open Command Prompt as an administrator.
  • Run the command below to replace the faulty System.Management.Automation.dll file.
copy C:\Temp\System.Management.Automation.dll "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll"
Replace System.Management.Automation.dll
  • Restart the device and launch PowerShell again. This time, PowerShell should open successfully without flashing or closing automatically.
  • Previously, there were also issues when launching PowerShell from Command Prompt. After replacing the DLL file, this issue was resolved as well. You can now start PowerShell directly from the Windows Command Prompt, and it should work correctly.
Test Powershell after reboot

Other Solutions

While the provided solution addresses a specific error message, if you’re experiencing a similar issue where the PowerShell console crashes or behaves unexpectedly, there may be alternative solutions to explore.

1. Uninstall and Reinstall PowerShell

The solution provided in this guide addresses a specific error related to PowerShell failing to launch due to a faulty or corrupted System.Management.Automation.dll file.

However, if you are experiencing a similar issue where the PowerShell console crashes, closes automatically, or behaves unexpectedly, there may be other possible causes. In that case, you may need to explore additional troubleshooting steps based on the exact error message and symptoms observed on the device.

Disable Powershell feature

Dism /online /Disable-Feature /FeatureName:"MicrosoftWindowsPowerShellV2Root"

Enable Powershell feature

Dism /online /Enable-Feature /FeatureName:"MicrosoftWindowsPowerShellV2Root"

To uninstall the PowerShell feature using the GUI, follow these steps:

  • Press the Win + R to open the Run dialog box.
  • Type appwiz.cpl and press Enter.
  • Click the Turn Windows features on or off link on the left-hand side in the Programs and Features window.
  • Scroll down the list to locate Windows PowerShell 2.0.
  • Uncheck the selection next to Windows PowerShell 2.0.
  • Click OK to confirm and begin the uninstallation process.
  • Restart your device after the feature is uninstalled.
Disable Powershell feature

2. Run the System file checker tool

You can use the System File Checker (SFC) tool to address issues with corrupted system files. Here’s the process, which involves running the Deployment Image Servicing and Management (DISM) tool before using SFC:

Run DISM Tool:

  • Open Command Prompt with administrator privileges.
  • Type the following command and press Enter:
DISM.exe /Online /Cleanup-image /Restorehealth

Run SFC Tool:

  • After the DISM tool has finished, run the System File Checker (SFC) tool.
  • In the same Command Prompt window, enter the following command and press Enter:
sfc /scannow
System file checker tool
sfc /scannow

Troubleshooting

1. Error code: 0x800f081f

After executing the DISM.exe /Online /Cleanup-image /Restorehealth command, you may get this error code.

The source files could not be found.
Use the Source option to specify the location of the files that are required to restore the feature. For more information on specifying a source location, see https://go.microsoft.com/fwlink/?LinkId=243077.

Note
DISM.exe /Online /Cleanup-image /Restorehealth
DISM.exe /Online /Cleanup-image /Restorehealth

This could be because of no internet connection or a firewall issue. You could try an offline restore method by copying the Windows folder from a working Windows computer to a faulty computer and providing it as a source to restore the files.

DISM Offline Windows System files repair

DISM.exe /Online /Cleanup-Image /RestoreHealth /Source:C:\RepairSource\Windows /LimitAccess

The LimitAccess parameter in DISM instructs the tool not to check Windows Update or Windows Server Update Services for the capability source files.

After completing a Windows repair using DISM command, run the System file checker tool to scan all protected system files and replace corrupted files with a cached copy in a compressed folder at C:\Windows\System32\dllcache.

2. Error while Copying System.Management.Automation.dll

While it’s straightforward to copy the System.Management.Automation.dll file from a functioning Windows device, you might encounter the following error message when attempting to paste it onto the destination device: The action can’t be completed because the folder or a file in it is open in another program. Close the folder or file and try again.

This issue can be resolved by taking ownership of the System.Management.Automation.dll file and grant administrators full control of the dll. The steps for taking ownership of this file are provided in the previous section of this post.

Error while copying System.Management.Automation.dll

Conclusion

This blog post explores methods for resolving PowerShell launch errors, primarily by replacing corrupted DLL files. We’ve also discussed running the system checker tool to repair corrupted system files. If the issue persists despite following the steps outlined in this post, you can consider backing up your data and performing a device reset, which will replace and fix any corrupted files.

Leave a Comment