Repository: microsoft/terminal (language: cpp)

Issue:
error 0x800700c1 when launching pwsh.exe
### Windows Terminal version (or Windows build number)

1.11.2921.0

### Other Software

pwsh.exe 7.2.0

### Steps to reproduce

my terminal has **C:\Users\Kevin Kostrzewa\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe** in the Powershell profile (note the space between **Kevin** and **Kostrzewa*() 

Installing Visual Studio 2022 created a file called **c:\users\Kevin** to be created, containing some installation logs.

Terminal failed to launch pwsh.exe after, presumably because of the space in the terminal profile around powershell. I think this is related to https://github.com/microsoft/terminal/issues/7195


### Expected Behavior

pwsh.exe to launch

NOTE: I think Terminal could be more resilient around pathing and launching the .exe so a file can't be injected ahead of it with clever space filled pathing. This *may* be a security issue, if c:\users\Kevin was attempted to be CreateProcess'ed.

### Actual Behavior

[error 0x800700c1 when launching `C:\Users\Kevin Kostrzewa\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe']


Discussion / hints:
I fixed this on my machine by killing the bad **c:\users\Kevin** file, so I'm not facing this issue right now.
Could you clarify- the commandline 

```
C:\Users\Kevin Kostrzewa\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe
```

did _you_ set that as the commandline in your profile, or are you using one of the dynamically generated `pwsh.exe` profiles that the Terminal creates for you?

> `[error 0x800700c1 when launching `C:\Users\Kevin Kostrzewa\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe']`

I suspect that implies that the Terminal did know that the exe was at that full path, but CreateProcess still decided to use `C:\Users\Kevin` as the executable path. Hmmm.
The command line was dynamically generated by Terminal
@kkostrzewa-coupa did you install PS7 manually? When adding PS7 from Microsoft Store this is what's being automatically inserted in `settings.json`

```
            {
                "guid": "{5fb123f1-af88-5b5c-8953-d14a8def1978}",
                "hidden": false,
                "name": "PowerShell 7",
                "source": "Windows.Terminal.PowershellCore"
            }
```

Tested on Win10 & WT Preview 1.12.2931.0


Interestingly, this is the same as what happens when you have a file called `C:\Program` and we try to launch something out of `C:\Program Files`...
@elsaco I believe I installed from the Microsoft store. Regardless, I think the core issue (weirdness around space in path) is still a problem.
CreateProcess, when given a string that doesn't contain quotes indicating the executable, is documented to probe and guess where the executable really is: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

Applications are  supposed to use quotes so CreateProcess doesn't guess, partly because if it does guess it's possibly a vulnerability.  It'd actually be nice to be able to globally disable this guessing behavior to preemptively flush out this class of bug.
@malxau from my read of that doc, the guessing occurs when lpApplicationName is NULL. Can lpApplicationName be set to the string in the Terminal Command Line?
I'd suspect no, since people could have a commandline like `wsl -d Ubuntu`, where `wsl` should be the application name. 

Presumably though, there's a way we could construct the commandline for the powershell profiles such that they'd be resistant to that. Maybe just wrapping them up in another layer of quotes?

Fix patch:
diff --git a/src/cascadia/TerminalSettingsModel/PowershellCoreProfileGenerator.cpp b/src/cascadia/TerminalSettingsModel/PowershellCoreProfileGenerator.cpp
index 15048022b9e..c55ef41da55 100644
--- a/src/cascadia/TerminalSettingsModel/PowershellCoreProfileGenerator.cpp
+++ b/src/cascadia/TerminalSettingsModel/PowershellCoreProfileGenerator.cpp
@@ -309,7 +309,15 @@ void PowershellCoreProfileGenerator::GenerateProfiles(std::vector<winrt::com_ptr
     {
         const auto name = psI.Name();
         auto profile{ CreateDynamicProfile(name) };
-        profile->Commandline(winrt::hstring{ psI.executablePath.native() });
+
+        const auto& unquotedCommandline = psI.executablePath.native();
+        std::wstring quotedCommandline;
+        quotedCommandline.reserve(unquotedCommandline.size() + 2);
+        quotedCommandline.push_back(L'"');
+        quotedCommandline.append(unquotedCommandline);
+        quotedCommandline.push_back(L'"');
+        profile->Commandline(winrt::hstring{ quotedCommandline });
+
         profile->StartingDirectory(winrt::hstring{ DEFAULT_STARTING_DIRECTORY });
         profile->DefaultAppearance().ColorSchemeName(L"Campbell");
         profile->Icon(winrt::hstring{ WI_IsFlagSet(psI.flags, PowerShellFlags::Preview) ? POWERSHELL_PREVIEW_ICON : POWERSHELL_ICON });


Test patch: