Running PowerShell in Visual Studio

Image Description

Daily PowerShell #5

Scripting Tools Daily PowerShell

October 22, 2021

quote Discuss this Article

Visual Studio has various ways of executing PowerShell scripts. There are several terminals and the PowerShell Tools for Visual Studio that offer ways to run PowerShell scripts. The options in this post work for Visual Studio 2017, 2019 and 2022.

Package Manager Console

The Package Manager console has been part of Visual Studio for many versions. It’s original intention was for executing cmdlets like Install-Package to add NuGet packages to projects within Visual Studio. That said, it is hosting a PowerShell runspace and you can execute arbitrary PowerShell commands.

The Package Manager console uses Windows PowerShell x86 in Visual Studio 2017 and 2019. The console uses Windows PowerShell x64 in Visual Studio 2022.

You can open the Package Manager Console by navigating to View \ Other Windows \ Package Manager Console.

Developer PowerShell Console

The Developer PowerShell Console was added in Visual Studio 2019. It provides a PowerShell console experience that is configured for Visual Studio developers. The environment is setup to include many of the paths and environment variables you would expect when running commands in the VS environment.

You can open the Developer PowerShell console by navigating to View \ Terminal.

The Developer PowerShell console runs in an external PowerShell process.

PowerShell Tools for Visual Studio

PowerShell Tools for Visual Studio is an extension you can install in Visual Studio to run PowerShell scripts. It provides a terminal, rich debugging tools and project system support.

Interactive Window

The PoshTools interactive window is a PowerShell terminal that you can use to execute scripts within Visual Studio. By default, you can use Windows PowerShell x86 and x64. With a license, you can use PowerShell 7 and the integrated environment that runs within the Visual Studio process. The integrated environment is the only configuration that runs within Visual Studio. All other configurations run in external PowerShell processes.

You can open the interactive window by pressing Ctrl+Shift+\ or by navigating to View \ PowerShell \ PowerShell Interactive Window.

Project System

You can also run PowerShell scripts by creating a PowerShell project within Visual Studio. To create a project, click File \ New \ Project and select the PowerShell language. Then, select the PowerShell Script project. A new project will be defined that contains a PS1 file.

You can open the PS1 file and add your commands to this file. When you want to execute the script, you can press F5 or click the Start button. Output will be shown in the output window (Ctrl+Alt+O) and the interactive window (Ctrl+Shift+\)

PowerShell scripts run in this manner support setting break points and debugging the script. You can set a break point by pressing F9 on the line you’d like to set a breakpoint or by clicking in the margin left of the line.

Solution Folder

You can also open folders that contain PowerShell scripts and execute them without creating a project. To open a solution folder, click File \ Open \ Folder. This will display all the files in the folder. If you open a PS1 file, you will be able to run it by pressing F5 or by clicking the Start button.

PowerShell scripts run in this manner support setting break points and debugging the script. You can set a break point by pressing F9 on the line you’d like to set a breakpoint or by clicking in the margin left of the line.