Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

shell - How can I start Powershell script as noprofile in Visual Studio Code

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • If you're running PowerShell in the PowerShell Integrated Console, which is a a special shell that comes with the PowerShell extension:

    • To turn off profile loading in this special shell, make sure that the
      PowerShell: Enable Profile Loading option for the PowerShell extension is unchecked (via File > Preferences > Settings, Ctrl-,).

    • See the bottom section for how to control which particular PowerShell edition / executable is used in the PowerShell Integrated Console.

  • If you're running PowerShell as a general-purpose shell in Visual Studio Code's integrated terminal:

    • You'll have to modify the default PowerShell shell profile or add a custom profile, with an "args" argument value of [ "-noprofile" ], by direct editing of the JSON file underlying the settings, settings.json (>Preferences: Open Settings (JSON) from the command palette).

    • The following shows a relevant settings.json excerpt with a modified default PowerShell profile that suppresses profile loading.

"terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell",
      "args": [ "-noprofile" ]  // suppress profile loading
    },  // ...
}

Read on for detailed, general information about shells and terminals in Visual Studio Code.


Overview of shell and terminal settings in 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.
    • The default shell for opening an external terminal (> Open New External Terminal); note that on Windows you can specify a shell executable directly, but on Unix-like platforms you must specify a terminal application, which itself then determines the shell to launch - see details below.

  • 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 default shells are:

    • For the integrated terminal and running tasks:
      • Windows: PowerShell.
        • Note that if a PowerShell (Core) 6+ version is found to be installed, it takes precedence over the built-in Windows PowerShell edition.
      • Unix-like platforms: the user's default shell, as reflected in the SHELL environment variable.
    • For the external terminal:
      • Windows: conhost.exe, which launches cmd.exe (Command Prompt)
      • Unix-like platforms: the host platform's default terminal application, such as Terminal.app on macOS, which itself determines what shell to launch (though, by default, it is also the user's default shell).
      • Note: On Windows only you may specify a shell (rather than a terminal) executable directly (e.g., bash.exe), in which case it opens in a regular console window (conhost.exe)

The following excerpt from a Settings.json file (> Preferences: Open Settings (JSON)) shows the relevant settings for each (as of VSCode v1.60 / PowerShell Extension v2021.8.2):

  • In earlier VSCode versions, the "terminal.integrated.shell.*" and "terminal.integrated.shellArgs.*" settings determined the default shell and its startup arguments for the integrated terminal. These have been superseded by shell profiles, defined via "terminal.integrated.profiles.*" properties, and an associated "terminal.integrated.defaultProfile.*" property that contains the name of the profile to use by default, as shown below.
{ 

  // ...

  // **General-purpose integrated-terminal shell**.

  // Shell *profiles* define the *available* shells for the integrated terminal.
  // This property is situationally created automatically, platform-appropriately,
  // based on what shells VSCode finds in standard locations on your 
  // system.
  // However, it *need not be present* in a given file - VSCode
  // knows about about *standard* profiles *implicitly* when it
  // comes to choosing a default shell.
  // This example applies to Windows, and shows that Git Bash
  // was found on the system.
  // On Unix-like platforms, replace ".windows" with ".osx" or ".linux", 
  // as appropriate.
  // To add custom profiles:
  //   * In file *paths*, use "\" or "/" as the path separator.
  //   * Use an "args" array property to specify start-up arguments, if necessary.
  "terminal.integrated.profiles.windows": {
      "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
      },
      "Command Prompt": {
        "path": [
          "${env:windir}\Sysnative\cmd.exe",
          "${env:windir}\System32\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
      },
      "Git Bash": {
        "source": "Git Bash"
      }
  }

  // Define the *default* shell profile, which serves as the default
  // shell in the *integrated terminal* and - except
  // if overridden, as shown below - also for *tasks*.
  "terminal.integrated.defaultProfile.windows": "PowerShell"  
  
  // **Automation-tasks shell**,
  // for the tasks defined in "tasks.json" and for debugging:
  // This definition is *optional* and *overrides* the default shell configured above.
  // Note: 
  //  * The *executable file path* must be specified (just the file name is sufficient for executables present in %PATH%);
  //    that is, this setting doesn't reference the shell *profiles*.
  //  * There is NO way to pass startup arguments.
  "terminal.integrated.automationShell.windows": "cmd.exe",

  // **External-terminal executable**:
  // The *terminal program* to use for opening an external terminal window, which itself determines what shell to launch.
  // (> Open New External Terminal).
  // Note: 
  //  * The *executable file path* must be specified (just the file name is sufficient for executables present in %PATH%);
  //  * There is NO way to pass startup arguments.
  //  * This example specifies Windows Terminal (wt.exe).
  //   * On Windows only, you may also specify a *shell* executable directly,
  //     which then opens in a regular console window (conhost.exe)
  "terminal.external.windowsExec": "wt.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., PowerShell version selector)

    • 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).
  • 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 P

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...