u20210512
u20210512

Reputation: 53

What do these PowerShell Core Parameters Mean?

In Windows 10 File Explorer, at what appears to be the recovery drive of this computer (I assigned drive letter R), I selected "PowerShell 7" > "Open here as Administrator," and when the prompt to allow the app to make changes to this device popped up, I read the Program Location entry as:

"C:\Program Files\PowerShell\7\pwsh.exe" -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "R:\!" -Command "$host.UI.RawUI.WindowTitle="PowerShell 7 (x64)"

What is the meaning of each parameter and value?

Thank you

Upvotes: 1

Views: 357

Answers (1)

mklement0
mklement0

Reputation: 437823

PowerShell (Core) 7's CLI, pwsh, is documented in the conceptual about_Pwsh topic (analogously, Windows PowerShell's CLI, powershell.exe - a subset of the former - is documented in about_PowerShell.exe).

The linked help explains the meaning of -NoExit (keep the session open), -WorkingDirectory (the startup working directory) and -Command (the command to execute on startup, which sets the console window's title).

However, a parameter that isn't documented is
-RemoveWorkingDirectoryTrailingCharacter, whose sole purpose is to work around a syntax problem
:

  • In File Explorer / Desktop shortcut-menu definitions (defined via the registry), the folder at hand is represented as %V, which is expanded by the Windows (GUI) Shell to that folder's full path on invocation.

  • While folders other than the root folder of a given drive are represented without a trailing \ (e.g., c:\foo), root folders of necessity do end in \, e.g, C:\

  • Since the shortcut-menu definition encloses %V in "..." so that the PowerShell CLI correctly recognizes folder paths even if they contain embedded spaces when passed to -WorkingDirectory (-wd), an expanded argument such as "C:\" would break the invocation syntax, because the trailing \" is interpreted as an escape sequence, i.e., as an escaped " char. rather than a verbatim \ followed by a (syntactic) closing ".

The workaround is to insert an auxiliary dummy character - ! - after %V, before the closing ", which avoids the syntax problem - but also preserves the dummy character.

The undocumented -RemoveWorkingDirectoryTrailingCharacter switch therefore instructs the CLI to simply strip the last character - the aux. dummy character - from the -WorkingDirectory argument received, yielding the original argument.


As you've discovered via this comment on GitHub, a simple tweak to the command line would be sufficient to obviate the need for -RemoveWorkingDirectoryTrailingCharacter:

  • Change "%V" to "%V\."

While this expands to something like "C:\\." for root folders, the doubled \ are benign (if you're passing a literal root-folder path, "C:\\" would be sufficient; the extra \ then serves to escape the closing ").

Upvotes: 1

Related Questions