Skip to content

Add 'Restart Windows Explorer' command, implemented using Restart Manager #39258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jiripolasek
Copy link

Summary of the Pull Request

  • Adds a new "Restart Windows Explorer" command to the "Windows System Commands" provider
  • Implemented using Restart Manager to allow restoring previously opened Explorer windows after restart
    • This depends on the "Restore previous folder windows at logon" option in File Explorer Options
    • An explicit timeout was added for terminating processes, since Restart Manager uses very long timeouts 😴
  • The shell process name (explorer) is hardcoded
  • The command attempts to terminate all explorer processes indiscriminately
  • Execution requires confirmation (if enabled in settings)

image

PR Checklist

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

@jiripolasek
Copy link
Author

@microsoft-github-policy-service agree

This comment has been minimized.

@zadjii-msft zadjii-msft added the Product-Command Palette Refers to the Command Palette utility label May 6, 2025

This comment has been minimized.

@jiripolasek jiripolasek force-pushed the feature/39213-restart-explorer-command branch from 33b3253 to dfe135a Compare May 8, 2025 12:34
Restricts process restarts to only affect the processes associated with the current session. This change prevents cross-session interference and supports multi-user scenarios more reliably.
@jiripolasek jiripolasek marked this pull request as ready for review May 12, 2025 15:10
@yeelam-gordon yeelam-gordon added this to the PowerToys 0.92 milestone May 16, 2025
@yeelam-gordon yeelam-gordon requested review from moooyo and Copilot May 16, 2025 06:50
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a new “Restart Windows Explorer” command to the Windows System Commands provider, leveraging the Restart Manager API to gracefully terminate and relaunch explorer.exe.

  • Introduces localization entries for command name, description, confirmation
  • Implements ShellRestartHelper to orchestrate process shutdown/restart via Restart Manager
  • Updates command registry, icons, and native interop definitions

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Properties/Resources.resx Added resource strings for the new restart command
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ShellRestartHelper.cs New helper class to restart explorer.exe using Restart Manager
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/NativeMethods.cs Added P/Invoke signatures and structs for Restart Manager APIs
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Icons.cs Added an icon entry for the shell restart command
src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/Commands.cs Registered the Restart Windows Explorer command
.github/actions/spell-check/expect.txt Whitelisted “rstrtmgr” for spell-checker
Files not reviewed (1)
  • src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Properties/Resources.Designer.cs: Language not supported
Comments suppressed due to low confidence (2)

src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Helpers/ShellRestartHelper.cs:26

  • There are no unit tests covering ShellRestartHelper. Adding tests for the restart logic (e.g., mocking NativeMethods) would improve confidence in this complex behavior.
internal static async Task RestartAllInCurrentSession()

src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/Properties/Resources.resx:427

  • [nitpick] The command name value is too generic. Consider using a more descriptive label like "Restart Explorer" to match the title and avoid UI ambiguity.
<value>Restart</value>

@@ -110,6 +111,13 @@ public static List<IListItem> GetSystemCommands(bool isUefi, bool hideEmptyRecyc
});
}

results.Add(new ListItem(new ExecuteCommandConfirmation(Resources.Microsoft_plugin_sys_RestartShell_name!, confirmCommands, Resources.Microsoft_plugin_sys_RestartShell_confirmation!, static () => Task.Run(static () => ShellRestartHelper.RestartAllInCurrentSession())))
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary use of Task.Run inside the command callback may complicate error propagation and cancellation. Consider passing the async method directly (e.g. static () => ShellRestartHelper.RestartAllInCurrentSession()).

Suggested change
results.Add(new ListItem(new ExecuteCommandConfirmation(Resources.Microsoft_plugin_sys_RestartShell_name!, confirmCommands, Resources.Microsoft_plugin_sys_RestartShell_confirmation!, static () => Task.Run(static () => ShellRestartHelper.RestartAllInCurrentSession())))
results.Add(new ListItem(new ExecuteCommandConfirmation(Resources.Microsoft_plugin_sys_RestartShell_name!, confirmCommands, Resources.Microsoft_plugin_sys_RestartShell_confirmation!, static () => ShellRestartHelper.RestartAllInCurrentSession()))

Copilot uses AI. Check for mistakes.

Comment on lines +36 to +41
await RestartProcessesInCurrentSessionAsync("explorer.exe");

// - Windows can automatically restart the shell if it detects that it has crashed. This can be disabled
// in registry (key AutoRestartShell).
// - Restart Manager is not guaranteed to restart all the processes it closes.
await EnsureProcessIsRunning("explorer.exe");
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The executable name "explorer.exe" is hard-coded in multiple places. Extract it to a private constant to avoid duplication and potential typos.

Suggested change
await RestartProcessesInCurrentSessionAsync("explorer.exe");
// - Windows can automatically restart the shell if it detects that it has crashed. This can be disabled
// in registry (key AutoRestartShell).
// - Restart Manager is not guaranteed to restart all the processes it closes.
await EnsureProcessIsRunning("explorer.exe");
await RestartProcessesInCurrentSessionAsync(ExplorerExecutableName);
// - Windows can automatically restart the shell if it detects that it has crashed. This can be disabled
// in registry (key AutoRestartShell).
// - Restart Manager is not guaranteed to restart all the processes it closes.
await EnsureProcessIsRunning(ExplorerExecutableName);

Copilot uses AI. Check for mistakes.

{
ArgumentException.ThrowIfNullOrWhiteSpace(processExecutableName);

var restartManagerSessionHandle = nint.Zero;
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The P/Invoke signatures use IntPtr but the code uses nint for session handles. For consistency and clarity, consider using IntPtr throughout.

Suggested change
var restartManagerSessionHandle = nint.Zero;
var restartManagerSessionHandle = IntPtr.Zero;

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Product-Command Palette Refers to the Command Palette utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants