如何在 Visual Studio Code 中以 noprofile 的形式启动 Powershell 脚本 [英] How can I start Powershell script as noprofile in Visual Studio Code
问题描述
如何在 Visual Studio Code 中将 Powershell 脚本作为 noprofile 启动,我可以使用命令 PowerShell_Ise -NoProfile 运行带有 noprofile 的 Powershell Ise.但是我们如何在 Visual Studio Code 中为 poershell 会话做同样的事情.
在 Visual Studio Code 的上下文中:
内在地应用了 3 种不同类型的外壳:
集成终端的默认外壳(面板的
TERMINAL
标签).- 可选,自动化外壳用于自动化任务(在
tasks.json
中定义)而不是默认的集成终端外壳.
- 可选,自动化外壳用于自动化任务(在
用于打开外部终端的默认shell(
>打开新的外部终端
)
使用 )
- 选择一个不同的版本(如果存在),前缀为
Switch to:
- 如果未显示感兴趣的版本/版本,您必须通过
Settings.json
文件添加其可执行路径(请参阅下一点).
- 选择一个不同的版本(如果存在),前缀为
通过
settings.json
(>首选项:打开设置(JSON)
):数组值
powershell.powerShellAdditionalExePaths
属性允许您添加扩展无法自动找到的 PowerShell 版本的完整可执行路径 - 请参见下面的示例.
立>powershell.powerShellDefaultVersion
属性决定了默认使用的版本;由于您必须通过其显示名称指定它,其中包括为自动发现的版本自动选择的显示名称,因此最简单的方法是通过 GUI 进行选择,如上所示.
<代码>{//...//扩展无法自动发现的任何 PowerShell 可执行文件的路径.//versionName"是自选名称,在//点击版本号时弹出的版本选择器菜单//靠近状态栏的右边缘时//PowerShell 集成控制台处于活动状态.//(当前激活版本以其实际特性显示,//不是通过它的versionName"属性;例如.,//"PowerShell 7.0 (X64) 核心版 [7.0.0]")powershell.powerShellAdditionalExePaths":[{"versionName": "最新预览","exePath": "C:\\Users\\jdoe\\AppData\\Local\\Microsoft\\powershell\\pwsh.exe"}],//默认情况下要使用的 PowerShell 可执行文件的versionName"属性.//注意:要切换到扩展自动找到的可执行文件,//使用版本选择器菜单最简单."powershell.powerShellDefaultVersion": "最新预览",//...}
How can I start Powershell script as noprofile in Visual Studio Code, I can run the Powershell Ise with noprofile with command PowerShell_Ise -NoProfile . But howcan we do the same for a poershell session in Visual Studio Code.
In the context of Visual Studio Code:
3 different types of shells apply intrinsically:
The default shell for the integrated terminal (the
TERMINAL
tab of the panel).- Optionally, the automation shell to use for automation tasks (defined in
tasks.json
) instead of the default integrated-terminal shell.
- Optionally, the automation shell to use for automation tasks (defined in
The default shell for opening an external terminal (
> Open New External Terminal
)
With the PowerShell extension installed, yet another shell applies:
- The specific PowerShell executable to use for the PowerShell Integrated Console, which provides tight integration with editing and support for debugging PowerShell code.
These shells:
- can all be configured separately
- may differ in their default behavior
- only some of them allow you to specify startup parameters, such as
-NoProfile
in your case.
The following excerpt from a Settings.json
file (> Preferences: Open Settings (JSON)
) shows the relevant settings for each (as of VSCode v1.42 / PowerShell Extension 2020.3.0):
{
// ...
// **General-purpose integrated-terminal shell**:
// Mere file *names* work for executables that are in %PATH%; e.g., "cmd.exe"
// If you need to specify a file *path*, use "\\" or "/" as the path separator.
// On Unix-like platforms, replace ".windows" with ".osx" or ".linux",
// as appropriate.
"terminal.integrated.shell.windows": "powershell.exe",
// Startup parameters to pass to the specified shell.
// On Unix-like platforms, replace ".windows" with ".osx" or ".linux",
// as appropriate.
"terminal.integrated.shellArgs.windows": "-NoProfile",
// **Automation-tasks shell**,
// for the tasks defined in "tasks.json" and for debugging:
// This overrides the default shell configured above.
// Note: There is NO way to pass startup arguments.
"terminal.integrated.automationShell.windows": "cmd.exe",
// **External-terminal shell**:
// The executable to use for opening an external terminal window
// (> Open New External Terminal).
// Note: There is NO way to pass startup arguments,
// so you cannot suppress profile loading, for instance.
"terminal.external.windowsExec": "powershell.exe",
// **PowerShell Integrated Console**:
// Profile loading is *disabled* by default; you can enable it here, but
// note that the PowerShell Integrated Console has its own,
// separate $PROFILE location, which differs from the one in a
// regular console window. If you want to load your regular profile,
// place the following statement in the $PROFILE file of
// the Integrated Console:
// . ($PROFILE -replace '\.VSCode', '.PowerShell')
// (Open the profile file for editing by submitting the following command
// from the Integrated Console:
// code $PROFILE
// )
"powershell.enableProfileLoading": false,
// ...
}
If you want to configure the PowerShell Integrated Console to use a different PowerShell edition / version:
GUI method: With the PowerShell Integrated Console active in the
Terminal
tab in VSCode's panel (lower half of the screen), click on the version number icon in the bottom right corner (e.g., )- Select a different version, if present, prefixed by
Switch to:
- If the version / edition of interest isn't shown, you must add its executable path via your
Settings.json
file (see next point).
- Select a different version, if present, prefixed by
Via
settings.json
(> Preferences: Open Settings (JSON)
):The array-valued
powershell.powerShellAdditionalExePaths
property allows you to add the full executable paths of PowerShell versions that the extension couldn't find automatically - see example below.The
powershell.powerShellDefaultVersion
property determines which version to use by default; since you must specify it by its display name, which includes the automatically chosen display name for auto-discovered versions, it's simplest to make the selection via the GUI, as shown above.
{
// ...
// The paths to any PowerShell executables that the extension cannot auto-discover.
// The "versionName" is a self-chosen name that is offered in the
// version-selector menu that pops up when you click on the version number
// near the right edge of the status bar when the
// PowerShell Integrated Console is active.
// (The currently active version is displayed by its actual characteristics,
// not by its "versionName" property; e.g.,
// "PowerShell 7.0 (X64) Core Edition [7.0.0]")
"powershell.powerShellAdditionalExePaths": [
{
"versionName": "Latest Preview",
"exePath": "C:\\Users\\jdoe\\AppData\\Local\\Microsoft\\powershell\\pwsh.exe"
}
],
// The "versionName" property of the PowerShell executable to use by default.
// Note: To switch to an executable that the extension found automatically,
// it is simplest to use the version-selector menu.
"powershell.powerShellDefaultVersion": "Latest Preview",
// ...
}
这篇关于如何在 Visual Studio Code 中以 noprofile 的形式启动 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!