Visual Studio Code import library references for Powershell extension - powershell

I have Powershell files in the same folder with a number of helper functions. Unfortunately the Visual Studio Code editor does not recognize those functions when using intellisense. How do I import reference libraries and get VSCode to search those files ?

If you want the code available to intellisense you have to load it into one of two places, either the PowerShell Integrated Terminal or your editor. In your case though, if you need it loaded all the time I would simply dot source those files in the VS Code PS Profile. You can create this simply by going to the PS Integrated Terminal and using New-Item $PROFILE -ItemType File -Force and it will create a Microsoft.VSCode_profile.ps1. Anything you add in that file will get loaded into the editor and PS Integrated Terminal upload startup.
More details on the whole engine and how intellisense works in VS Code...
The reason this works is that the main engine in the PowerShell extension is the PowerShellEditorService. This is the engine that creates the host behind the PS Integrated Terminal, and the PS features you have in the editor itself (e.g intellisense, script analyzer, etc.).
You can see an example of this by adding Import-Module somemodule to a new file in VS Code that has not been loaded into the PS Terminal. It can take a few seconds but after the engine loads the module you will have intellisense for that module. Just like you will your code.
Prior to adding the commands in that editor I verified that the module was not loaded. After adding the import command and then using a command from that module the module gets loaded in the background. You can then see the module was loaded:

Related

setting up visual studio code for c++, but unable to compile

I have installed visual studio 2019 which works fine for c++ but unable to create folders and sections to store multiple code files. So thought of switching to visual studio code and followed all tutorials available on youtube and documentation. But Visual studio IDE has got nothing showing up when Run Build task is initiated, which is expected to show the c++ extension downloaded. Mingw64 is installed and checked in the terminal too.
no output or terminal opened on Run
compiler installation checked
From the image attached regarding no output in the terminal or terminal opened, it looks like the exe is made because it says 'Build finsihed succesfully'. You'll need to run this executable from the terminal.
For this, if youre in the same directory all you have to do is type the filename. helloworld.exe in the cmd, if its powershell, then ./helloworld.exe. (Also note that for exe files you dont need to explicitly mention .exe at the end of the file name)
The powershell method should work on cmd too.
And if youre not in the same directory as the executable, you can either change directory by typing cd path/to/file and proceed as above, or run directly by typing path/to/file/helloworld.exe. where path/to/file is the directory of your helloworld.exe file

Can I detect in Powershell that I am running in VS Code's integrated terminal?

I would like to modify the standard PowerShell profile in Windows if the Powershell opens inside VS Code integrated terminal (when you are editing e.g. python scripts in VS Code, rather than PS scripts, which opens the ISE profile in any case).
Is there some environmental variable that gets set by the integrated PowerShell? Or is there some way of opening Powershell with a particular profile, instead of the default?
Thanks
VS Code creates an environment variable named TERM_PROGRAM. You can check it for a value of vscode, something like this:
if($env:TERM_PROGRAM -eq 'vscode') {
# do some stuff...
}
If you want to check if you're running within PowerShell Integrated Console (ships with PowerShell extension) under vscode and not just any powershell console running under vscode, you can:
if ($Host.Name -eq 'Visual Studio Code Host') {
Write-Output 'PowerShell Integrated Console'
}
This is meaningful to detect because it is the only powershell console host that provided full debugger support (eg. break on exception experience) and debugger integration with vscode.
You said you're into modifying profile when running within vscode, then you should check (again with PowerShell integrated console which ships with PoweShell extension):
PS> $PROFILE.CurrentUserCurrentHost
C:\Users\username\Documents\PowerShell\Microsoft.VSCode_profile.ps1
PS> $PROFILE.AllUsersCurrentHost
C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1
If people are working on PowerShell code within Visual Studio Code, then why they would NOT install PowerShell extension which ships with a specific console that fully integrates with Visual Studio code and on the top of that, provides you a profile file specifically geared towards Visual Studio code?

'code' is not recognized as an internal or external command - Microsoft Visual studio Code

I am using visual studio code for develop my react application. I just wanted to open the project in the IDE using CLI, using "code ." command. Although it was perfectly working previously, now shows an error as below.
'code' is not recognized as an internal or external command,
operable program or batch file.
Can someone help me out to solve this matter?
This sounds like an issue with your environment variables rather than a problem with VS Code itself.
When you run the code command in Windows Command Prompt (cmd), Windows searches all commands on your path for one named code, and executes that command. VS Code installs a command with this name that opens the editor, normally located here on Windows:
C:\Program Files\Microsoft VS Code\bin
It may be that your path no longer contains this particular directory for some reason.
You can edit your path directly to add it:
From the Start menu, type "variables" and choose the option to "Edit environment variables for your account".
Locate the PATH entry and edit it.
Add the directory where the code command exists, e.g. C:\Program Files\Microsoft VS Code\bin.
The path editor may appear slightly different depending on what version of Windows you are running.
My case was different rather than general solution for this question. I saw that the number of solutions which says the probelm exists with the environment valriables. In the new release it provides a setup file only.
Before downloading, there are certain properties to be selected.When I just redownloaded the set up and run it , it starts working perfectly.

How to Import Nuget Module to powershell script [duplicate]

I was trying to use my favorite source control from the Package Manager console in Visual Studio 2010. I had issues described in another topic. For now the best answer is to move all command-line stuff from Visual Studio to bare PowerShell.
But this case the Visual Studio related commandlets are not working. For example, most crucial one — I cannot run Update-Database command from Entity Framework.
Is it possible to register somehow Visual Studio specific commandlets for the current project in PowerShell? Or make PowerShell automatically take current project context from the packages subfolder?
The NuGet PowerShell commands rely on being run from within Visual Studio so will not work outside in the normal PowerShell running from the command line.
You can however use migrate.exe which ships with the EntityFramework NuGet package and use that from the command line to update your database.
As a prototype I put together a way to use NuGet PowerShell commands from the normal PowerShell command line using SharpDevelop. Unfortunately at the moment the EntityFramework NuGet package does not work with SharpDevelop.
Another interesting project is StudioShell which provides a new DTE: drive inside Visual Studio but can also be used outside from the command line. I do not believe it supports NuGet PowerShell commands being run from the normal PowerShell command line.
I can find the NuGet.psd1 file at:
C:\Program Files (x86)\Microsoft Visual Studio
12.0\Common7\IDE\Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1.
However, when you try to load it:
PS> Import-Module $pathToNuGetPsd1 -Force -NoClobber -Scope Global
Import-Module : The name of the current Windows PowerShell host is: 'ConsoleHost'.
The module 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\
Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1' requires the following Windows
PowerShell host: 'Package Manager Host'.
I think we're out of luck. It has to be run from the Package Manager Host and requires things from Visual Studio as stated by Matt.
To solve my problem I used Chocolatey to install NuGet.CommandLine and then used NuGet.bat to do what I needed. It is a little more work and may not work in all cases depending on what you're trying to do.
Chocolatey: https://github.com/chocolatey/chocolatey/wiki/Installation
NuGet.CommandLine:
PS> cinst NuGet.CommandLine
I'm not terribly familiar with the Visual Studio cmdlets, but you can import a module into your PowerShell session by using Import-Module -Name <ModuleName>. You can list the available (aka. "installed") PowerShell modules by using `Get-Module -ListAvailable'.
My guess would be that the Visual Studio cmdlets are contained with its own PowerShell module, but it's quite possible that it's not "installed" to one of the standard locations in $env:PSModulePath. If this is the case, then you might need to locate the module directory and import either the .psd1 or .psm1 file directly, and pass that into: Import-Module -Name <FullPathToModuleFile>.
As an example of the above, take notice of where the Windows Azure PowerShell module is located: http://trevorsullivan.net/2012/06/07/introducing-microsofts-official-windows-azure-powershell-module/
It's under the Program Files directory, and is not immediately available to PowerShell, unless you import the module from its fully qualified path (the .psd1 module manifest file).
Hope this helps.

Run powershell script at startup in visual studio

I have a powershell file (script.ps1) that is in some of my visual studio solutions. The script contains utility functions for the project.
At the moment, I just open the nugget package manager and copy and paste the content of the script and it runs fine. I can run the functions correctly from the nugget package manager. But this has to be done, each time I open the solution.
Is there a way to get this handle automatically each time I open the solutions?
Rather than copy pasting the entire thing, you can dot source the file
. ".\script.ps1"