Skip to content

Pass command to PowerShell at startup #4360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
o-l-a-v opened this issue Jan 5, 2023 · 6 comments
Closed
2 tasks done

Pass command to PowerShell at startup #4360

o-l-a-v opened this issue Jan 5, 2023 · 6 comments
Labels
Area-Startup Issue-Enhancement A feature request (enhancement).

Comments

@o-l-a-v
Copy link
Contributor

o-l-a-v commented Jan 5, 2023

Prerequisites

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

Summary

Unlike existing issue #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 o-l-a-v added the Issue-Enhancement A feature request (enhancement). label Jan 5, 2023
@ghost ghost added the Needs: Triage Maintainer attention needed! label Jan 5, 2023
@SeeminglyScience
Copy link
Collaborator

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

@o-l-a-v
Copy link
Contributor Author

o-l-a-v commented Jan 5, 2023

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.

@egerlach
Copy link

egerlach commented Jan 7, 2023

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

@o-l-a-v
Copy link
Contributor Author

o-l-a-v commented Jan 7, 2023

@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.

@andyleejordan
Copy link
Member

@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 andyleejordan added Area-Startup and removed Needs: Triage Maintainer attention needed! labels Jan 31, 2023
@o-l-a-v
Copy link
Contributor Author

o-l-a-v commented Jun 16, 2024

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 o-l-a-v closed this as completed Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Startup Issue-Enhancement A feature request (enhancement).
Projects
None yet
Development

No branches or pull requests

4 participants