Powershell and VSCode Terminal issue - powershell

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.

Related

PowerShell 5.1 in Visual Studio code behaves strangly

When using PowerShell in VS code and working interactivly, often when I highlight some code and run it then it does not print anything in the terminal. Then I simply restart The "PowerShell Integrated Console" (by pushing the trash can) and it then works again.
It is quite annoying. My ADS also acts like this. Is this a general problem with PowerShell with VS Code or is it my computer?
My VS Code version is 1.68.0. My PowerShell version is 5.1.19041.1682. And the version of the PowerShell extension is v2022.5.1

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.

How to have powershell display the current directory

I recently ran "conda init powershell" on my powershell in order to be able to activate conda environments (previously, "activate env_name" would not work).
However, I do not like what this has done to my powershell. For one, it always displays the current virual environment even if it is simply the base environment. Also, it no longer displays the current directory I am in.
I tried running commands like conda deinit, but it does not exist.
I tried removing anaconda from the path and restart the computer, but it did not work and powershell still recognized conda as a cmdlet.
I tried changing defaults from powershell, but it only lets me change Options, Font, Layout, and Colors. None of which are helpful.
Minimal reproducible example. With "pathtoanaconda/Scripts" in path, open PS and run "conda init powershell", restart powershell.
Notice in my screenshot how my current directory is not displayed.. Instead, I get a "(base) PS" for any directory I am in1
Oh.. I quickly realized https://superuser.com/questions/446827/configure-the-windows-powershell-to-display-only-the-current-folder-name-in-the has instructions that solve my problem.
I had to delete the information on that file in order to undo the conda init.

VS Code: How to disable Security Warning for 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.

Visual Studio Code Powershell extension does not recognize profile while debugging

I’m using the Powershell extension for Visual Studio Code. I updated the profile:
C:\Users\xxxxxxxx\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1
To include some functions and variables that I want to make available to other scripts. When I reference a variable from the profile within another script, it does not appear that the profile has been loaded. I suspect this because the variable value is blank when I query it from the VS Code console. If I run the same test from the standard Powershell console with an associated profile, the variable value is resolved.
Can anyone tell me what, if anything, I need to do to use a Powershell profile in VS Code while debugging?
There are several profiles. The starting point is the four locations that you can find by reading the following properties of PowerShell's built-in $profile variable.
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
$profile.AllUsersCurrentHost
$profile.AllUsersAllHosts
As noted in this article by The Scripting Guy, because Windows has both Powershell and the Powershell ISE, you have at least two possible values of Current Host, so at least 6 profiles.
I've tested this in the Visual Studio Code terminal window, and it seems that the "CurrentHost" profiles are the same as you get by simply running a powershell instance. I'd assume then that Code isn't seen as a distinct host, and just runs a normal powershell.
Once you've got that far, there's another possible complication, which is that the AllUsers profiles are down in C:\Windows\System32 and hence on a 64 bit system, also mirrored in C:\Windows\SysWOW64\. So depending on whether you are using a 32 or 64 bit editor, and whether the Powershell is hosted in a 32 or 64 bit process, it is quite possible that the file you are editing has no influence on the Powershell.
Inside your debug session run: $profile. This will return the path to currently used profile file, so you can make your changes there.
Alternatively you could change the system wide profile in C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
With the just released version 0.10.0 of the VS Code PowerShell extension debugging with a previously loaded profile.ps1 being available is now implemented. Note that the interactive console and the debugger share the same PS session.
If you type $profile in the "PowerShell Integrated Console" of VSCode, you will see the path to the profile used:
C:\Users\xxx\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1
Now if you type $profile in a normal terminal, you will see your "normal" profile: C:\Users\xxx\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Now the easiest solution that did the trick for me is to create a hard link between the two files (the VSCode_profile.ps1 file didn't exist in my case):
mklink /H Microsoft.VSCode_profile.ps1 Microsoft.PowerShell_profile.ps1
Reload VSCode and you are all set!