Does Visual Studio Code work with PowerShell in the Command Palette? - powershell

With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.
I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?
Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.

This was the first problem I wanted to solve in VSCode. I did not find a way to
type PowerShell commands but I have found a way to create and run PowerShell tasks.
In the command palette type and choose Tasks: Configure Task Runner. It will
show you a json file configured for the default task runner.
I replaced the content with this
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.
P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.

With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.

Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).

Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.
In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...
The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.
Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:
Now you can debug PowerShell scripts through VSCode.
The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/
EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode

As of version 0.10.3 there is now a PowerShell extension written by Microsoft that will enable writing PowerShell with IntelliSense and highlighting in Visual Studio Code
http://blogs.msdn.com/b/powershell/archive/2015/11/17/announcing-windows-powershell-for-visual-studio-code-and-more.aspx

At least in V1.4 (July 2016) this has been made a lot simpler.
The relevant documentation can be found here:
https://code.visualstudio.com/docs/editor/integrated-terminal
Essentially all you're doing is adding an entry to the settings.json file for User Settings.
Adding the following value:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
IMG: Shows the User Settings settings.json file and the successful Integrated Console showing the PowerShell prompt:

An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.
Keyboard Shortcut to open Terminal: CTRL + `
Sources:
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/docs/getstarted/keybindings
I confirmed in version 1.19.2. Not sure when the feature was first integrated.

There is a much easier way to run PowerShell, no configuration needed:
Install the Code Runner Extension
Open the PowerShell code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will run and the output will be shown in the Output Window.
Besides, you could select part of the PowerShell code and run the code snippet. Very convenient!

PowerGUI tool provides a nice interface to Visual Studio. The goal of this extension is to bring PowerShell development into Visual Studio.
Here is how it looks -
Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.
PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
PowerShell code snippets: The code snippet feature can be used for PowerShell code.
PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed.
Download Here
It also provides Debugging facility which is the best part I like as a developer.
Thanks!

Related

How to add a run button in visual studio code?

i am a newcomer to this site and i'm not english so excuse me if this post isn't as good as you expected. Let's get to the point: to give you an example of what i mean with "run button", take the python one for example.
Once you install python extension in visual studio code, you can press a button in the right top of the screen, and then vsc automatically executes a command in a shell running python on the active .py file.
Given this example, I'd like to know how to make a button for vsc that automatically creates a new terminal, executes a command which i would insert in the making of the button and nothing else, of course the terminal shall not disappear after giving the desired output.
Just to give you some more info, i'm on ubuntu, if that helps anyhow.
Feel free to tell me whether i should edit this question in anyway.
Install the Task Runner extension. Now, every task you add to tasks.json will be displayed as a button, in the 'Task runner' panel (which is normally on the same tab as the file explorer).
Here's a decent tasks.json template:
{
"version": "2.0.0",
"tasks": [
{
"label": "COMMAND NAME",
"type": "shell",
"command": "YOUR COMMAND HERE",
"presentation": {"echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true},
},
// More tasks here, if you need them.
]
}
Put this file in your project directory, in a directory called .vscode. Open the project directory with "File" -> "Open Folder".
I know you did not ask for debugging, but you can use the debugger launch mechanism on arbitrary programs. You can define launch.json to implement any number of launch configurations. The currently selected configuration will display a button you can click on.
https://code.visualstudio.com/docs/editor/debugging
Install Code Runner extension in VS Code
you can add the Code Runner extension.
This will show a run button on top of the editor panel
Add Code Runner Extension and you will see a RUN BUTTON on the Top Right in VS code. Like this

How to use ``OUTPUT`` in VSCode?

I've always used Sublime Text 3 for Python Coding, in which Build Systems were the only way of running a file.
Now I'm starting C++ programming, and I'm using VSCode. The problem I have is that, in that dock where:
PROBLEMS , OUTPUT , DEBUG CONSOLE , TERMINAL
The only thing I've had trouble with, is OUTPUT window, because nothing ever appears, so if I wanna run either Python or C++ code, it won't show anything to let me know what output would the program give. And if I just try to run it (pressing f5) it, it will open terminal and run it from it.
I saw something called Tasks but I don't know how they work, and they seem to be like Build Systems from Sublime Text 3.
I wanna use OUTPUT window, how can I get it working?
to run code in the output section of visual studio code you can use the extension Code Runner
just install, then press CTRL + ALT + N to run the file (supports c++ & python)
I wanna use OUTPUT window, how can I get it working?
As it stands now, you can not use the Output Window as a way to log code, that is reserved for native logs like Window, Main, Shared or Extensions to log their output using channels. There is a good candidate called the Debug Console.
You can log to the Debug Console using a launch.json configurable:
First, create a launch.json for your program.
Then add the following properties, in addition to whatever you need to get your program going:
internalConsoleOptions
Controls when the internal debug console should open
redirectOutput
Both of these used in tandem will open the 'Debug Console' instead of terminal and just provide the necessary output; though it still sends to terminal should you still want it:
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"internalConsoleOptions": "openOnSessionStart",
"redirectOutput": true
}
]
For c++ they have an extensive tutorial on how to debug and run: https://code.visualstudio.com/docs/languages/cpp (in addition to setting up launch.json and tasks.json files respectively https://code.visualstudio.com/docs/cpp/config-mingw#_build-helloworldcpp)
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
ext install formulahendry.code-runner

How to run Lua script from within VS Code

Have been using Notepad++ for awhile now, and adding scripts via Lua extensions. Now, I would like to get my feet wet using VS Code and was wondering what sort of extensibility I could leverage in that environment? Possible to run the same Lua scripts, for instance? Or are there other avenues I should consider? Thanks for any insights!
Please install the vs-code extensions "LuaDebug" and "extensionPath" provided by actboy168.
Add a file named .vscode/launch.json with the content:
{
"version": "0.2.0",
"configurations": [
{
"type": "lua",
"request": "launch",
"name": "Launch",
"program": "${workspaceFolder}/src/main.lua"
}
]
}
if your source to be executed is src/main.lua.
Then hit F5 or "Run"->"Start Debugging", select the lauch configuration and happy debugging.
An example is also shipped by the author of the plug-in (actboy128), which can be found here: https://github.com/actboy168/luamake
Please do not try to build it, simply load it in VS Code and debug it!
I'm using the following settings to run the interpreter from VSCode using the menu option "Terminal|Run Build Task":
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Lua",
"type": "shell",
"command": "lua54",
"args": ["${file}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The file contains this settings must be named as "tasks.json". I suggest you to read the VSCode in order to learn how to create it properly.
NOTE: in the command property, where you read "lua54", you must write the name of the lua interpreter executable without the extension.
You would have to configure a task file to run lua from VS code and install lua on your pc if it's isn't done yet.
In order to debug lua you would have to install the extension lua debug which will add a ton of useful feature to help you debug lua such as breakpoint.
Your question seems to touch on several topics:
How to run a Lua script from VS Code: other answers here have already addressed it.
How to extend VS Code: pull up the extension view in VS Code, find an extension you like, and click install. More info here:
https://code.visualstudio.com/docs/editor/extension-marketplace
How to author VS Code extensions: they are written in TypeScript or JavaScript rather than Lua. More info here:
https://code.visualstudio.com/api/get-started/your-first-extension
you can type the following in cmd and it will run the command. Instead of installing extensions.
"C:\Program Files\LOVE\love.exe" "D:\game"
--> this is the path where your main.lua file is

How to run Powershell x86 within Visual Studio Code x64?

I've got a situation where I am using the 64-bit version of Visual Studio Code to write/debug a powershell script. However, because of what the Powershell script is doing it needs to run within the 32-bit version of Powershell. It's using some libs to access an MS Access file, so I have yet to find a way to make things work within Powershell x64.
Is there a way to tell VS Code to run the 32-bit version of Powershell if VS Code itself is running as 64-bit? For example, can I modify the launch.json file to specify the path of powershell itself?
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File",
"script": "${file}",
"args": [],
"cwd": "${file}"
},
...
]
}
I found another method that's easier and seems to be provided by VSCode (although it might be an extension I added). In the main window there's a clickable element in the right side of the toolbar:
When you click that a menu appears near the top of the window with some powershell related options, including the ability to switch between x86 and x64:
Assuming you have the PowerShell extension installed, you should be able to modify the powershell.powerShellExePath setting in VS Code Settings to "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe".
I believe you can also set "powershell.useX86Host": true. This was introduced in the PowerShell extension v0.5.0; I'm not sure how I missed it's inclusion!
However, it might be easier or better to install the 64-bit MS Access components and just use the 64-bit version.
Microsoft Access Database Engine 2010 Redistributable
Microsoft Access 2013 Runtime
Microsoft Access Database Engine 2016 Redistributable
I've no idea why the 2013 version has a different name, but as far as I can tell those are the same components for the different versions.
You can click the '{}' text in the bottom bar, then click on "Show PowerShell Session Menu".
This should give some different Powershell .exe options (x86 etc.) to choose from, depending on what is installed on the system.

Can VSCode (Visual Studio Code) be used to run elm-make?

I am new to VSCode.
I am new to ELM.
I am perfectly capable of using VIM and command line tools to create an ELM Project, but I want to utilize an IDE. I have chosen VSCode on advice from the internet since it seems to pick up some nice pieces of VIM.
So now I have a few .elm files.
Main.elm
View.elm
Model.elm
I want to run elm-make on Model.elm to make sure it has no errors.
Then I want to run elm-make on Main.elm to create an index.html so I can view my project.
I think this is a pretty simple question for people familiar with how to customize VSCode, but as I stated previously, I am new to VSCode.
Try setting up a task for elm-make:
Create a ./vscode/tasks.json with the contents:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "elm make",
"isBuildCommand": true,
"command": "elm-make",
"args": ["./main.elm"],
"isShellCommand": true
}
]
}
You can then use the build command to run the task, or run the task individually.
You may also want to look into the elm extension: https://marketplace.visualstudio.com/items?itemName=sbrink.elm