vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Pass command to PowerShell at startup

Open o-l-a-v opened this issue 2 years ago • 5 comments

Prerequisites

  • [X] I have written a descriptive issue title.
  • [X] I have searched all issues to ensure it has not already been reported.

Summary

Unlike existing issue https://github.com/PowerShell/vscode-powershell/issues/1470 which asks for the ability to pass parameters to powershell.exe, I'd like the ability to pass PowerShell command(s) to whatever PowerShell version is used behind the scenes by the extension.

For the VS Code built in terminals, but also in Windows Terminal, I like to add following command at startup to quickly be able to identify what PowerShell version is used:

"'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})"

For VSCode integrated terminals I add the following in settings.json:

Click to view
"terminal.integrated.profiles.windows": {
    "PowerShell Microsoft Store": {
        "args": [
            "/NoLogo",
            "/NoExit",
            "/NoProfile",
            "/ExecutionPolicy",
            "RemoteSigned",
            "/Command",
            "'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})"
        ],
        "icon": "terminal-powershell",
        "overrideName": true,
        "path": "${env:LOCALAPPDATA}\\Microsoft\\WindowsApps\\Microsoft.PowerShell_8wekyb3d8bbwe\\pwsh.exe"
    },
    "Windows PowerShell (x64)": {
        "args": [
            "/NoLogo",
            "/NoExit",
            "/NoProfile",
            "/ExecutionPolicy",
            "RemoteSigned",
            "/Command",
            "'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})"
        ],
        "overrideName": true,
        "path": "${env:SystemRoot}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
    },
    "Windows PowerShell (x86)": {
        "args": [
            "/NoLogo",
            "/NoExit",
            "/NoProfile",
            "/ExecutionPolicy",
            "RemoteSigned",
            "/Command",
            "'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})"
        ],
        "icon": "terminal-powershell",
        "overrideName": true,
        "path": "${env:SystemRoot}\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe"
    }
}

For Windows Terminal it can be done like so:

Click to view
...
"profiles": 
{
	...
	"list": 
	[
		...
		{
			"commandline": "\"%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" /NoLogo /NoExit /Command \"'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})\"",
			"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
			"hidden": false,
			"name": "Windows PowerShell x64"
		},
		{
			"commandline": "\"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\Microsoft.PowerShell_8wekyb3d8bbwe\\pwsh.exe\" /NoLogo /NoExit /Command \"'v{0} x{1}' -f $PSVersionTable.PSVersion.ToString(),$(if([System.Environment]::Is64BitProcess){'64'}else{'86'})\"",
			"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
			"hidden": false,
			"name": "PowerShell Microsoft Store",
			"source": "Windows.Terminal.PowershellCore"
		},
		...
	]
}
...

Proposed Design

A similar config.json settings for the PowerShell extension would be nice. Something like powershell.integratedConsole.startupCommand maybe?

  • If powershell.integratedConsole.startupCommand is defined and has a value: <powershell/pwsh>.exe /Command <value_of_powershell.integratedConsole.startupCommand>

o-l-a-v avatar Jan 05 '23 09:01 o-l-a-v

Any particular reason you'd prefer this over using a profile.ps1?

SeeminglyScience avatar Jan 05 '23 16:01 SeeminglyScience

Just preference I guess. Haven't really used profile.ps1, ever. Maybe to avoid "it works on my machine", not assuming profile.ps1 does something I don't have to handle in scripts I make.

Also, when actually opening powershell.exe from run prompt for instance, I know that it's Windows PowerShell x64. So for me at least, having version and architecture in profile.ps1 would be redundant, or at least not be that useful.

Also, VSCode settings.json is very easily synced between devices.

o-l-a-v avatar Jan 05 '23 16:01 o-l-a-v

While searching I found #190 about Workspace Profiles. Would that solve this issue?

egerlach avatar Jan 07 '23 18:01 egerlach

@egerlach

Sounds like that would tie it to a VSCode workspace? I'd like it not to be like that.

Maybe vscode-powershell could just print what version and architecture is used when it starts up? Or show those details somewhere in the VSCode GUI? Optionally, so that those who'd like to see those details can enable it, disabled by default.

o-l-a-v avatar Jan 07 '23 19:01 o-l-a-v

@o-l-a-v those details are readily available in the language status icon:

Screenshot 2023-01-09 at 9 59 25 AM

Full version and architecture of the PowerShell executable used to launch the Extension Terminal.

andyleejordan avatar Jan 09 '23 18:01 andyleejordan

I don't have the need for this anymore. I've started using a central profile.ps1 synced with OneDrive, then local profiles points to it like so:

<#
    .EXAMPLE
        & $(Try{$psEditor.GetEditorContext().CurrentFile.Path}Catch{$psISE.CurrentFile.FullPath})
#>
[OutputType([System.Void])]
Param()
& ('{0}\_Share\AppData\PowerShell\Profile.ps1' -f $env:OneDriveConsumer)

o-l-a-v avatar Jun 16 '24 17:06 o-l-a-v