Visual Studio Code Powershell extension does not recognize profile while debugging - powershell

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!

Related

Saving Aliases for powershell in Visual Studio code

I'm pretty new to both visual studio code and PowerShell, mostly used gitBash before. I'm just wondering if there is a way to save aliases that I set in the powershell, so I don't have to redo them everytime I restart the application.
I tried a few google searches but could not find the answer to my question.
Thank you.
S
If you're using Visual Studio Code with the PowerShell extension, you're using the PIC (PowerShell-Integrated Console) in the Visual Studio Code's integrated terminal, which has its own $PROFILE file, separate from stand-alone PowerShell installations.
You can open this $PROFILE file from the PIC as follows:
psedit $PROFILE
Any alias definitions or functions you place in there will be available in future PIC sessions by default - but only there.
Alternatively, to share definitions with a stand-alone PowerShell installation, dot-source its $PROFILE file; e.g., to dot-source:
the current user's Windows PowerShell profile, use:
. ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
the current user's PowerShell (Core) 7+ profile, use:
. ~\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

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.

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.

Where does the version number in PowerShell title refer to?

In this post I found the way to detect the installed Powershell version: Determine installed PowerShell version.
When I type "powershell" into "Execute as" (Win+R) the powershell opens with the title:
C:\Windows\System32\WindowsPoershell\v1.0\powershell.exe
However, the commands from the linked SO question deliver following output:
So my question: Why is there v1.0 in the title of the shell window whereas the other commands tell me that I have version 2?
Your PowerShell version is 2. The title shows the path to the PowerShell executeable. I think the reason why all new PowerShell version are located there (in v1.0) is because so many cmd scripts invoking some PowerShell code using this file path (e. g. Scheduled Tasks). But I may be wrong.
Another reason could be, that its not possible to run two different PowerShell Versions side-by-side.

Why won't PowerShell ISE let me set breakpoints when running as Administrator?

Having failed to get PowerGUI to work properly I've fallen back on PowerShell ISE. However if I run this as Administrator it won't let me set any breakpoints.
If I launch as normal (my login is a member of Administrators) all is well but because I need to script some tasks that require full administrator rights this is no good.
I have saved the script as a proper file (i.e. it's not "untitled1.ps1") but no joy.
I'm running Windows 7 x64 Ultimate.
Strangely this doesn't seem to be a problem on my Windows 2008 R2 dev box (I'm logging in as user that is a member of Administrators then running PS ISE "As Administrator" and breakpoints are set and hit.
Any ideas why this would be?
I also faced the same issue, saving the file to a physical location enabled all debugging capabilities.
It looks like there's a problem with PowerShell and square brackets in folder and filenames.
Both PowerGUI and PowerShell ISE won't hit breakpoint if the script being debugged resides in a folder with [ or ] in the name.
I notice you're running x64. Is there any chance that you're running x64 ISE but have only set the execution policy away from restricted on x86 (or vice-versa).
I am using Visual Studio Code and in my case, I was missing the powershell extension.
Was facing the same issue because I had used other editor to make changes to the ps1 file. After opening the file with PowerShell ISE and clicking "Save" solved the issue for me.