VS Code: How to disable Security Warning for PowerShell - powershell

I've been trying to use VS Code a bit more lately as a replacement for Notepad++ but there is one feature in VS Code that is super annoying. I'll be typing away on a PowerShell .ps1 file (either locally on my work machine or on a file from another server via UNC path) and a Security Warning will pop-up at the top:
And after a few seconds a PowerShell Output pops open at the bottom:
I'm not sure why these windows keep popping open. I'm assuming my PowerShell script is running in the background or something, but how do I disable these windows? This is the main reason why I switch back to Notepad++ or use Atom.
BTW: I do not want my PowerShell scripts to run within VS Code while I'm coding.

Related

VS Code Integrated terminal does not execute commands from extensions

Ideally, the integrated VS Code terminal, depending on the context, the type of the folder and the extension, executes some commands. For example, when opening a folder containing a Python virtual environment in VS code, the environment is recognized and activated (by the python extension) by default when opening a new integrated terminal instance (situation 1). This is done by running some command similar to source /path/to/venv/bin/activate.
Or, when using the ROS extension to debug nodes, selecting "Start Debugging (F5)" uses the launch.json file to start some nodes and finally starts debugging the desired code. To do so also, there is some command that is executed (also by he ROS extension, I assume) in the integrated terminal (situation 2) to start the debugging process. In case of debugging ROS nodes, the command usually looks something like /usr/bin/env /bin/sh /tmp/someFileName.
But, unfortunately, both of the above mentioned situations fail. I believe this happens because while the extension tries to run these two commands within their respective integrated terminals, the commands do not actually get executed in either situation. Instead, these commands are printed on the top of the terminal, but the state of the terminal is unchanged (as opposed to when the commands would have been executed, in which case depending on the commands some actions are performed). Here are two images to show what I mean. Top, situation 1 and bottom, situation 2.
The fact that these two commands are printed on top of the terminal as soon as the a new terminal instance is opened tells me that the extension tries to execute them, but they do not work for some unknown reasons.
Just to be clear, both of them are run in a seperate VC Code window, they have nothing to do with each other. When I manually run both the commands in their respective terminals I do get the desired results.
Now, I am unsure exactly how to name this issue. But I think this is surely an issue with the integrated terminal, and not a problem of the extensions. I am not sure how one could reproduce this problem.
I did a clean reinstall of VS code by deleting %APPDATA%\Code and %USERPROFILE%\.vscode. Because I am using this on WSL, there is only ~/.vscode-server on the ubuntu side. I manually uninstalled all extensions on WSL but did not delete this folder, in fear of breaking something. The problem still persisted. I have also created an issue on the VS Code GitHub page with nearly the same information.
I am unsure if this is a bug or is there something wrong with my settings. Does anyone know how I could fix this? For smaller use-cases I can still manually enter the command in the terminal. But I am trying to debug a ROS application with nearly 10 different terminals opening up and I cannot be manually entering the command each time to restart the process.
Please let me know if you need any more information. Many thanks in advance.
Edit: both edits to frame the question properly.
Although not related to WSL, I dug a little deeper today as to why in my case the extension commands were not being executed or were being chopped.
I'm an iTerm2 user. iTerm2 has something called Shell Integrations, which allow iTerm to behave differently under certain circumstances, for example, adding markers to each prompt or coloring output with certain text (e.g. WARNING or ERROR)
From time to time, I also use the VSCode Integrated Terminal, which recently added support for reporting whether the previous command errored out with an indicator on the gutter of the Integrated Terminal panel using the exit code.
iTerm can do something similar but the shell integrations mess up completely the VSCode functionality and therefore I changed my .bashrc file to detect if the terminal emulator was iTerm2 or not (which can be done with the it2check utility of iTerm2) so that it only sourced the shell integrations if I was using iTerm2.
The problem is that it2check "eats" some STDIN bytes using dd, specifically, until it finds an n so that it can obtain the name of the emulator. This of course chops the commands on the STDIN until the first n and makes VSCode Extension Terminal commands unusable
The workaround I came up with is to use the value of "$TERM_PROGRAM" as means to distinguish between the different programs. The only caveat is that the value won't be passed if you're inside of a tmux session or similar, but I can live with that.
In your case, I'd check for any process that is either not passing the STDIN to the WSL process or any dot files or shell profile scripts eating up the STDIN they receive.
I suspect that the real problem is that the local process doesn't relay the STDIN contents to the WSL and as a workaround you may try to create a VSCode Integrated Terminal profile that uses SSH to connect to the WSL host so that the STDIN is preserved

Powershell and VSCode Terminal issue

I'm having an issue regarding the Powershell extension in VSCode. I am getting a blank line, and a different command for the script being executed.
I get this on vscode:
PS>
PS>. "C:\JulyCode\HelloWorld.ps1"
Hello World
PS>
On the ISE I currently get this:
PS> C:\JulyCode\HelloWorld.ps1
Hello World
PS>
I'd prefer to have the latter. Not sure if this is a workspace setting or what to troubleshoot. VSCode didn't do this before, and I imagine I made a change or the extension made a change. I've tried new workspaces, new settings (using my older but saved settings), etc and still had no luck. Also it seems to only run on the Extension: Powershell Integrated Console. Not sure if that can be changed either.

Copied-and-pasted tab characters not recognized by Powershell

Powershell ignores tab characters in copied-and-pasted code snippets. This is annoying because I like to write my Python code with single tabs instead of quadruple spaces, and in testing I frequently copy and paste snippets in to test them.
So far I've been using other terminals like Cygwin that properly address pasted tabs, but it would be more convenient for me to use Powershell. How can it be made to address these pasted tabs correctly?
The MWE is to copy and paste anything with a tab into Powershell. For example,
test
appears in Powershell as
>>> test
with no indentation. I've copied and pasted from both Notepad and Notepad++ with the same effect; I'm not using any formatted word processors or anything like that.
Caveat: The next section only applies to PowerShell's own interactive prompt. Different rules may apply to external programs launched from PowerShell that present their own interactive prompt, such as python and, preferably, ipython - see the bottom section for considerations specific to these programs.
Preserving tabs on pasting directly into PowerShell's interactive prompt:
Note: Strictly speaking, when it does work, tabs are converted to 4 spaces each on pasting.
Only works when all of the following prerequisites are met:
When running PowerShell (Core) 7+ (as opposed to Windows PowerShell)
When pasting via Ctrl-V rather than via right-click (the latter emulates typing and therefore triggers tab completion).
When running in one of the following console environments:
Regular console window (conhost.exe)
Windows Terminal
Note:
Does not work in Visual Studio Code's integrated terminal (which seemingly always emulates typing in PowerShell).
Haven't tried third-party consoles such as Cygwin and ConEmu.
Notably, this categorically excludes Windows PowerShell (where a tab ends up as ^I).
Preserving tabs on pasting into the interactive python / ipython REPL, from PowerShell or cmd.exe:
Note:
ipython provides a superior REPL experience compared to python.
Unlike the above, the following also applies to launching from Windows PowerShell, as well as from cmd.exe.
When it works, pasted tabs are converted to 4 spaces, except where noted otherwise.
ipython
Regular console windows (conhost.exe):
Works, with Ctrl-V only.
Windows Terminal:
Works, with Ctrl-V only.
Visual Studio Code's integrated terminal:
Does NOT work (strips tabs).
python
Regular console windows (conhost.exe):
Works, with both Ctrl-V and right-click, as long as Filter clipboard contents on paste is turned OFF on the Options tab of the console window's Properties dialog; pastes actual tabs.
Windows Terminal:
Works, with right-click only(!); pastes actual tabs.
Visual Studio Code's integrated terminal:
Works.
The only way I was able to make this work was by using Windows Terminal. Which is a new terminal application built by Microsoft that supports a bunch of new features.
I don't know the technical reasons why, but it appears it supports pasting both tabs and spaces appropriately, without loss or conversion into the Python CLI REPL.
I tested this using Windows Terminal while running Windows PowerShell, PowerShell Core 7+ and Command Prompt, while running the Python CLI and all supported pasting tabs.
You can install Windows Terminal through a number of sources...Windows Store, Binary download, WinGet, Choco, etc.
Here's the github repo with instructions on various ways to install.
https://github.com/microsoft/terminal
In general, control-v is better for pasting characters than right-click. Note control-v has to be bound to psreadline's paste function (so no emacs mode). Here's an example with an a and an em dash (0x2013). Right click will only paste the a.
a–

Powershell does not open using windows explorer shortcut

I usually use the shortcut described here: How do I start PowerShell from Windows Explorer? to open a PS session in a specific folder. Just using powershell to open the sessions
However is no longer working, every time I write powershell on the address bar and hit enter it take me to the folder: C:\Users\MyUser\Documents\PowerShell (I'm in a Windows 10 laptop)
It seems to be related only to this shortcut as I have tried a few other things and they work. List of things I have tried/checked already below:
powershell -noprofile it work opening a PS session as expected
No, my $Profile file does not have anything to set the location of the PS Session.
powershell.exe works as expected
powershell_ise works as expected
going to File --> Open Windows Powershell works as expected
Checked my environment variables and nothing strange there either
It seems to be related to the fact that I installed PS core 6 recently. This installation introduced the folder C:\Users\MyUser\Documents\PowerShell (it's a new folder vs the ones that existed with Windows Powershell) and that's why the windows explorer now opens a folder instead of the command line.
I suppose I will need to get used to powershell.exe instead of powershell from now on.

Using "Visual Studio code" with interactive Powershell window like Powershell ISE

I'm new to Visual Studio code so thanks in advance for the help.
I often use the PowerShell ISE with an interactive PowerShell window. I open it, type a few commands, then run a script (such as opening a PowerShell session with Office 365) and then type a few more commands.
Is there any way to do the same thing with Visual Studio code? I see a way to run scripts and debug them but not have them complete but the PowerShell window still stay around so I can continue working.
Decided to convert to answer, since this is a bit too much for comments:
Go to settings > user settings and change "terminal.integrated.shell.windows" to:
"C:\\WINDOWS\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
Ctrl + Shift + ` opens the terminal. Using the + button on the terminal window you can launch additional powershell instances.
Also there's an extension that allows for launching several different terminals under the same VSCode process (cmd,powershell,bash,etc). Minor nuance, shells are being started with the same permissions VSCode has, but you can re-elevate once inside the shell.
References:
https://code.visualstudio.com/docs/editor/integrated-terminal
https://code.visualstudio.com/docs/customization/userandworkspace
https://github.com/Microsoft/vscode/issues/10893
Edit: but I would say its not really convenient, terminal takes away too much space on the screen (well, unless you have like 2k or 4k display), so I use powershell + vscode.