When I right-click a ps1 script in VSCode and open it in Powershell Terminal, the current folder is not the script folder but the project folder.
Is there a way to configure it to open in the script folder ?
Assumptions:
You've configured PowerShell as the default shell - either via command Terminal: Select Default Shell (on Windows) or directly via user settings "terminal.integrated.shell.windows" / "terminal.integrated.shell.osx" / "terminal.integrated.shell.linux".
You're trying to open a PowerShell instance by right-clicking a file / subfolder in the side bar's Explorer view and choose Open in Terminal, and you want that instance's current location to be that file's containing folder / that subfolder.
As of (at least) VSCode 1.17.2, this should work by default.
If it doesn't, perhaps custom code in your $PROFILE directly or indirectly changes the current location.
If you don't want to / cannot prevent $PROFILE code from changing the location, here's a workaround via the user settings that explicitly sets the current location after the $PROFILE code has run:
On Windows:
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [ "/c", "powershell -noexit -command 'Set-location \"%CD%\"'" ]
This uses cmd.exe as the default shell, which then invokes PowerShell with a command that explicitly makes it change to that folder.
The workarounds for other platforms below work analogously:
On macOS:
"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [ "-l", "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]
On Linux:
"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": [ "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]
Related
I'm using the Platformio IDE terminal within Atom. It defaults to powershell, which works. I'm trying to use Anaconda prompt instead of cmd or powershell. The target of Anaconda Prompt and Anaconda Powershell are respectively:
%windir%\System32\cmd.exe "/K" C:\Users\myname\Anaconda3\Scripts\activate.bat C:\Users\myname\Anaconda3
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\Users\myname\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\Users\myname\Anaconda3' "
The terminal works when mapped to the cmd.exe or powershell.exe. It does not work when I add the rest of the target for the Anaconda prompts. I can run the following once the cmd.exe is open in Atom, and it works:
C:\Users\myname\Anaconda3\Scripts\activate.bat C:\Users\myname\Anaconda3enter
I add this into the Shell Arguments field in settings for Platformio, and it doesn't work. Based on the field description ("Specify some arguments to use when launching the shell"), I thought this would auto execute when opening the terminal. That does not appear to be happening. Anybody know how to use the shell arguments field appropriately or write a path for the Shell Override field that accurately boots up Anaconda Prompt/Anaconda Powershell? Thanks!
Notice in my VS Code, a fresh terminal, the very first line indicates it is properly defaulted to Powershell 7.2.2 - just as I believe it should.
But then in this same screenshot, the first script I run (which has a command to print out the powershell version) it shows the PSVersion is 5.1.19041.1320
Here's all my powershell specific VSCode entries from settings.json
"terminal.integrated.profiles.windows": {
"MyPowerShell_7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"args": ["-NoProfile"]
}
},
"terminal.integrated.defaultProfile.windows": "MyPowerShell_7",
"powershell.powerShellAdditionalExePaths": [
{
"exePath": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"versionName": "MyPowerShell_7"
}
],
"powershell.powerShellDefaultVersion": "MyPowerShell_7",
Why why why is it so hard to run the Test-Json cmdlet?
finally found an answer here on reddit
I didn't know that 5.1 and 7 are side-by-side and have different ways to invoke each application
So, if I change my command from powershell to pwsh I get the right result
Actually, when you have a PowerShell or pwsh terminal open (Which you have here) you should call your script directly and it will be executed in that host.
So instead of
PS C:\>pwsh -file './scripts/ps/experiment.ps1'`
you should call
PS C:\>./scripts/ps/experiment.ps1
The way you are calling your scripts will launch a new host, run the script and then terminate.
I recently downloaded the new Windows Terminal. I have created the shortcut for opening the multiple panes(which is working fine). However, I am trying to execute a command for the respective pane.
wt -d <path> -c "cls && php artisan serve" ;
split-pane -p "Command Prompt" -H -d <path> -c "npm run watch"
I googled for the solution but no luck.
Is this even possible..?
I got a similar setup working.
Im running Windows Terminal version 1.8.1444.0
My goal was to setup a dotnet core app running in left pane and a react-app running in right pane:
wt -d "C:\path\to\dotnetcoreapp" -p "Command Promt" cmd /k dotnet watch run ; split-pane -d "C:\path\to\reactapp" cmd /k yarn start
Also tried to start an interactive elixir session: wt -d "C:\dev\elixir" cmd /k IEx which also worked fine...
The short answer is: Yes it is possible but it is a workaround.
The Challenges
wt.exe does not currently have a command line option to execute a
command from a split-pane
wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run.
The workaround
To get around the first challenge we can launch a custom profile that executes the command via wsl.exe in the key value pair (in settings json) "commandline": "wsl.exe 'commands go here"
To get around the second challenge we need to execute the wsl.exe 'commands go here' via powershell.exe because Powershell has a -NoExit option which will keep the shell open after the command is executed. So for example if you wanted to open a shell that runs wsl.exe (your linux shell) with the command watch ps then the line in the custom profile would look like this:
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps"
The Solution:
Create a profile in Windows Terminal settings.json for each command you want to run. Each profile should have a unique guid that you can generate in Powershell by running the command [guid]::NewGuid(). So the profile to run the command watch ps would look something like this:
{
"guid": "{b7041a85-5613-43c0-be35-92d19002404f}",
"name": "watch_ps",
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps",
"hidden": false,
"colorScheme": "One Half Dark"
},
Now you can open a tab in windows terminal with two panes, the pane on the right will run the command watch ps and the shell will stay open. Put something like the below line of code in your shortcut (or from the command line) where the value of the option -p is equal to the value of the profile you created. Each additional pane you open will need a profile that has the command you want to run in it.
wt split-pane -p "watch_ps"
I'm trying to add anaconda prompt to start up instead of powershell to avoid having to add python to env variables.
"terminal.integrated.shellArgs.windows": [
<args>
]
I tried putting them into single line, splitting them "-Foo Goo" as well as "-Foo","Goo". Each version leads to either error or simply ignoring the "-Command" parameter (the lines simply get pasted, but not executed).
First of all, I I'd like to give a hint for everyone that uses PowerShell to use the new one.
So, with the Anaconda ready to go (and it been equal or greater than 4.6 - use conda --version) run in sequence (from base environment in cwd terminal):
conda update conda
conda init
This will update your conda root environment and the init will setup all you need to run it on both cwd and powershell.
After this, you can start any powershell (inside vscode or not) and it will be conda ready.
Look at this article for further information.
Hope it helps!
I ended up using this (although it has tendency to break).
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\ProgramData\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Anaconda3'"
],
Thanks Zerg! Your answer worked for me but I also got a warning message to say that this approach has been depreciated. After some googling I got this working by adding a new terminal profile to settings.json.
"terminal.integrated.profiles.windows": {
"PowerShell (Anaconda)": {
"source": "PowerShell",
"args": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3'"
]
}
},
Then changing the default profile:
"terminal.integrated.defaultProfile.windows": "PowerShell (Anaconda)",
I just installed PowerShell 7, and since I had anaconda installed before, this seems to add the starting command to the profile.ps1 automatically.
The profile.ps1 in C:\Users\USER\Documents\PowerShell (this is version 7, directory WindowsPowerShell would be the old version 5) contains:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
With these automatic settings at the start of PowerShell 7, adding PowerShell 7 as a new terminal type to vsccode solved it.
This is how to add PowerShell 7 to the dropdown menu:
Enter Ctrl+Shift+P, open settings.json for the User, and add
{
"terminal.integrated.profiles.windows": {
"PowerShell7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"args": ["-NoProfile",
"-noexit",
"-file",
"C:\\Users\\USER\\Documents\\PowerShell\\profile.ps1"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell7"
}
Then in the settings.json, press Ctrl+s and restart (!) vscode. You will see PowerShell7 as the new default terminal in the dropdown of terminal types:
From the VSCode Command Palette (Ctrl+Shift+P), select
Terminal: Select default shell
and then pick PowerShell.
Then from the Command Palette (Ctrl+Shift+P), select
Python: Select Interpreter
and pick one of the conda environments. When you now open a new terminal VSCode starts PowerShell and activates the selected environment. This is exactly what the Anaconda-Prompt does. However, you should not set a PYTHONPATH in the environment in combination with an Anaconda install. Conda activation is all you need. It not only adds the selected interpreter to the PATH, but the required libraries too.
Powershell 6 has a Unix-style /etc/issue that mentions a link to the docs.
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
This is fine, but:
I know where the docs are
I know I launched Powershell 6
How can I remove some, or all of the message? IIRC Powershell 5 still had the copyright message so maybe I can't remove that, but getting rid of the last 3 lines would help?
Pass the -nologo option.
-NoLogo Starts the PowerShell console without displaying the copyright banner.
pwsh.exe -nologo ...other arguments...
If you are using the new Windows Terminal then:
Go to Settings:
Add the argument -nologo to the PowerShell command line:
From #sandu's answer, it could be improved as below.
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": ["-noLogo"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
Now you can add -nologo in Windows Terminal settings:
The easiest way would be to add Clear-Host to the top of your $profile file.
You can open powershell and input $profile, you will get Microsoft.PowerShell_profile.ps1 file's path.
open this file (create one without) by notepad and input a clear or cls command.Save and exit.
the command of this file will be executed when you open powershell
As np8 said under this answer, the way to hide the initial text in the Visual Studio Code integrated terminal is as follows:
Open the workspace settings file (./.vscode/settings.json) or the user settings file (search for Preferences: Open Settings (JSON) in the command palette, opened with F1)
Add the following inside the outer-most "layer" of curly brackets:
"terminal.integrated.shellArgs.windows": [
"-nologo"
]
What worked for me was to go to File > Preferences > Settings and type terminal.integrated.defaultProfile.windows in the search bar (you can change windows with other supported OS if you need to). There should now be only one option to choose from (that's why I included windows). Click on the Edit in settings.json link bellow it. It should now open settings.json and generate the line necessary to modify the default terminal profile. Which looks something like:
"terminal.integrated.defaultProfile.windows": "",
Above that line you can create your own profile:
"terminal.integrated.profiles.windows": {
"PowerShell -nologo": {
"source": "PowerShell",
"args": ["-nologo"]
}
},
Name the profile however you want and add whatever parameters you want. Once you're done. Set the profile name into the option that was previously generated for you. In this example it should look like:
"terminal.integrated.defaultProfile.windows": "PowerShell -nologo",
Save and close. Now when you open a terminal it should have no logo for example.
I was fix problem on the top by
There are several ways of doing it :
Creating a shortcut with value
"C:\Program Files\PowerShell\7\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\Users\%username%"
If you are using Windows terminal then you can replace the source with
"commandline": "pwsh.exe -NoLogo"
Users of Chinese version can also add -nologo in Windows terminal settings:
As shown in the figure:
If you are using the new Windows Terminal in Windows 11,
Go to Settings:
Select the Windows Powershell profile from the left sidebar
Add the argument -nologo to the command line:
Save and be sure to restart your terminal to see the changes.
If you're using windows just create a profile and put the command clear inside it.
In PowerShell I'm going to open the profile with notepad:
notepad $Profile
Highly Recommended to Use
Powershell Preview: https://github.com/PowerShell/powershell/releases
Editing Context Menu
If you are using Powershell Preview, you can change context menu using regedit:
Open regedit
Go to Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShell7-previewx64
add flag -nologo to Icon
2. Editing Start Menu Shortcut
In File Explorer go to
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Create a shortcut with value
"C:\Program Files\PowerShell\7-preview\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\\Users\\%username%"