Skip to content

Commit 2bec074

Browse files
Fix: Use working directory when running PowerShell scripts (#16837)
1 parent 92e87fb commit 2bec074

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public RunWithPowershellAction()
3131

3232
public Task ExecuteAsync(object? parameter = null)
3333
{
34-
return Win32Helper.RunPowershellCommandAsync($"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'", PowerShellExecutionOptions.None);
34+
return Win32Helper.RunPowershellCommandAsync(
35+
$"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'",
36+
PowerShellExecutionOptions.None,
37+
context.Folder?.ItemPath
38+
);
3539
}
3640

3741
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
199199
await Win32Helper.OpenFormatDriveDialog(drivePath);
200200
break;
201201

202+
case "Windows.PowerShell.Run":
203+
await contextMenu.InvokeItem(
204+
menuId,
205+
contextMenu.ItemsPath[0].EndsWith(".ps1") ? Path.GetDirectoryName(contextMenu.ItemsPath[0]) : null
206+
);
207+
break;
208+
202209
default:
203210
await contextMenu.InvokeItem(menuId);
204211
break;

src/Files.App/Helpers/Win32/Win32Helper.Storage.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ public static string ExtractStringFromDLL(string file, int number)
405405
}
406406
}
407407

408-
public static async Task<bool> RunPowershellCommandAsync(string command, PowerShellExecutionOptions options)
408+
public static async Task<bool> RunPowershellCommandAsync(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
409409
{
410-
using Process process = CreatePowershellProcess(command, options);
410+
using Process process = CreatePowershellProcess(command, options, workingDirectory);
411411
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(30 * 1000));
412412

413413
try
@@ -432,11 +432,11 @@ public static async Task<bool> RunPowershellCommandAsync(string command, PowerSh
432432
}
433433
}
434434

435-
public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options)
435+
public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
436436
{
437437
try
438438
{
439-
using Process process = CreatePowershellProcess(command, options);
439+
using Process process = CreatePowershellProcess(command, options, workingDirectory);
440440

441441
process.Start();
442442

@@ -867,7 +867,7 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs
867867
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), PowerShellExecutionOptions.Elevated | PowerShellExecutionOptions.Hidden);
868868
}
869869

870-
private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options)
870+
private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
871871
{
872872
Process process = new();
873873

@@ -883,6 +883,10 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti
883883
process.StartInfo.CreateNoWindow = true;
884884
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
885885
}
886+
887+
if (workingDirectory is not null)
888+
process.StartInfo.WorkingDirectory = workingDirectory;
889+
886890
process.StartInfo.Arguments = command;
887891

888892
return process;

src/Files.App/Utils/Shell/ContextMenu.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task<bool> InvokeVerb(string? verb)
8585
return false;
8686
}
8787

88-
public async Task<bool> InvokeItem(int itemID)
88+
public async Task<bool> InvokeItem(int itemID, string? workingDirectory = null)
8989
{
9090
if (itemID < 0)
9191
return false;
@@ -100,6 +100,8 @@ public async Task<bool> InvokeItem(int itemID)
100100
};
101101

102102
pici.cbSize = (uint)Marshal.SizeOf(pici);
103+
if (workingDirectory is not null)
104+
pici.lpDirectoryW = workingDirectory;
103105

104106
await _owningThread.PostMethod(() => _cMenu.InvokeCommand(pici));
105107
Win32Helper.BringToForeground(currentWindows);

0 commit comments

Comments
 (0)