diff --git a/src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs b/src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs index 72700b45ce6b..29577f0cac9c 100644 --- a/src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs +++ b/src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs @@ -31,7 +31,11 @@ public RunWithPowershellAction() public Task ExecuteAsync(object? parameter = null) { - return Win32Helper.RunPowershellCommandAsync($"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'", PowerShellExecutionOptions.None); + return Win32Helper.RunPowershellCommandAsync( + $"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'", + PowerShellExecutionOptions.None, + context.Folder?.ItemPath + ); } private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) diff --git a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs index e97b64c744b8..39a5ed09af27 100644 --- a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs +++ b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs @@ -199,6 +199,13 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag) await Win32Helper.OpenFormatDriveDialog(drivePath); break; + case "Windows.PowerShell.Run": + await contextMenu.InvokeItem( + menuId, + contextMenu.ItemsPath[0].EndsWith(".ps1") ? Path.GetDirectoryName(contextMenu.ItemsPath[0]) : null + ); + break; + default: await contextMenu.InvokeItem(menuId); break; diff --git a/src/Files.App/Helpers/Win32/Win32Helper.Storage.cs b/src/Files.App/Helpers/Win32/Win32Helper.Storage.cs index 8e2e3a5979ba..c940884ccd8e 100644 --- a/src/Files.App/Helpers/Win32/Win32Helper.Storage.cs +++ b/src/Files.App/Helpers/Win32/Win32Helper.Storage.cs @@ -405,9 +405,9 @@ public static string ExtractStringFromDLL(string file, int number) } } - public static async Task RunPowershellCommandAsync(string command, PowerShellExecutionOptions options) + public static async Task RunPowershellCommandAsync(string command, PowerShellExecutionOptions options, string? workingDirectory = null) { - using Process process = CreatePowershellProcess(command, options); + using Process process = CreatePowershellProcess(command, options, workingDirectory); using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(30 * 1000)); try @@ -432,11 +432,11 @@ public static async Task RunPowershellCommandAsync(string command, PowerSh } } - public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options) + public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options, string? workingDirectory = null) { try { - using Process process = CreatePowershellProcess(command, options); + using Process process = CreatePowershellProcess(command, options, workingDirectory); process.Start(); @@ -867,7 +867,7 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), PowerShellExecutionOptions.Elevated | PowerShellExecutionOptions.Hidden); } - private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options) + private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options, string? workingDirectory = null) { Process process = new(); @@ -883,6 +883,10 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; } + + if (workingDirectory is not null) + process.StartInfo.WorkingDirectory = workingDirectory; + process.StartInfo.Arguments = command; return process; diff --git a/src/Files.App/Utils/Shell/ContextMenu.cs b/src/Files.App/Utils/Shell/ContextMenu.cs index 7fb485c7e9d8..b409ba730c03 100644 --- a/src/Files.App/Utils/Shell/ContextMenu.cs +++ b/src/Files.App/Utils/Shell/ContextMenu.cs @@ -85,7 +85,7 @@ public async Task InvokeVerb(string? verb) return false; } - public async Task InvokeItem(int itemID) + public async Task InvokeItem(int itemID, string? workingDirectory = null) { if (itemID < 0) return false; @@ -100,6 +100,8 @@ public async Task InvokeItem(int itemID) }; pici.cbSize = (uint)Marshal.SizeOf(pici); + if (workingDirectory is not null) + pici.lpDirectoryW = workingDirectory; await _owningThread.PostMethod(() => _cMenu.InvokeCommand(pici)); Win32Helper.BringToForeground(currentWindows);