man sitting at the table near the computer and three screens with the coding on it, flowers, and table with a cat near it
Fix Joseph Posted on 7:51 am

Setting Up PowerShell in Visual Studio Code

Visual Studio Code (VS Code) is a powerful and versatile code editor that has gained immense popularity among developers due to its extensibility and support for a wide range of programming languages. One of the many languages it supports is PowerShell, Microsoft’s scripting and automation framework. Setting up PowerShell in Visual Studio Code can significantly enhance your scripting and automation workflows. In this article, we’ll walk you through the steps to set up PowerShell in Visual Studio Code and highlight some essential extensions to supercharge your PowerShell development experience.

Visual Studio Code: Your Ultimate Code Editor

Visual Studio Code, often abbreviated as VS Code, is a versatile, open-source code editor designed for a multitude of purposes. Compatible with Windows, Linux, and macOS, it provides a seamless development experience and boasts built-in Git support.

However, what about the PowerShell ISE?

Once a beloved environment for many developers, the PowerShell Integrated Scripting Environment (ISE) has reached its development endpoint and will no longer receive updates or additions. The “Windows” or FullCLR version of PowerShell will follow a similar fate, limited to security patches from here on.

The torchbearer for PowerShell’s future is PowerShell Core, an open-source platform that functions seamlessly across diverse operating systems, including the likes of Raspberry Pi.

In this era of dynamic PowerShell development, embracing Visual Studio Code is a game-changer, propelling you into the ranks of code ninjas.

Getting Started with Visual Studio Code Extensions

Visual Studio Code (VS Code) offers a wealth of extensions to enhance your coding experience. Let’s walk through the process of installing extensions, starting with a popular one, the PowerShell extension.

Step 1: Launch Visual Studio Code

Begin by opening Visual Studio Code on your computer.

Step 2: Access the Extensions Marketplace

To install extensions, navigate to the Extensions Marketplace by following these steps:

Click on the Extensions icon located on the left sidebar. It resembles a square with four smaller squares inside.

Step 3: Search for and Install the PowerShell Extension

Now, we’ll find and install the PowerShell extension:

  • In the Extensions Marketplace search bar, type “PowerShell” and press Enter;
  • The search results will display the PowerShell extension. Click on it to learn more;
  • To install the extension, simply click the “Install” button.

By following these steps, you can effortlessly install extensions in Visual Studio Code, customizing it to meet your specific coding needs.

You might observe that it also includes a light theme, which you can choose to use if you prefer brighter themes. However, we will be installing a theme pack later to provide you with a broader selection of options.

Installing vscode-icons for Visual Studio Code

Now, let’s proceed to install an icon pack called “vscode-icons.”

Step 1: Access the Extension Marketplace

To begin the installation process, follow these steps:

Click on the Extensions icon, which is represented by a square icon with four smaller squares inside.

Step 2: Search for and Install vscode-icons

Next, we’ll find and install the vscode-icons extension:

  • In the Extensions Marketplace search bar, type “vscode-icons” and press Enter;
  • When the search results display the vscode-icons extension, click on it to learn more;
  • To install the extension, click the “Install” button;
  • After installation, make sure to activate and set the theme as per your preference.

Exploring Themes

Now, let’s delve into the world of themes. I personally favor the Rainglow theme pack, and I suggest beginning with it.

Step 1: Access the Extension Marketplace

To initiate the theme installation process, follow these steps:

Click on the Extensions icon, represented by a square with four smaller squares inside.

Step 2: Search for and Install Rainglow Theme

Next, let’s locate and install the Rainglow theme:

  • In the Extensions Marketplace search bar, type “Rainglow” and press Enter;
  • Once you find the Rainglow theme, click on it to explore further;
  • Install the theme by clicking the “Install” button;
  • After installation, set the theme according to your preference.

Step 3: Setting the Theme

To access and explore themes, utilize the keyboard shortcuts provided below based on your operating system:

  • For Windows/Linux: Press CTRL+SHIFT+P;
  • For macOS: Press Command+SHIFT+P;
  • This will open the Command Palette. Ensure that the “>” character appears on the left side, indicating that you are ready to search. Now, go ahead and search for various themes to discover the one that suits your preferences the best. For a swift selection, you can start by searching for “Codecourse Contrast (rainglow).

Checking Your PowerShell Version on Windows

character in gray t-shirt sitting at the laptop and smiling, code behind him

 

If you are using Windows, you can easily identify your current PowerShell version by looking in the lower right-hand corner of the screen. If you see a version number there, it means you can edit and debug PowerShell code in the Desktop/5.1 environment. For non-Windows systems, this version information may not be visible since no default version is installed.

Visual Studio Code simplifies the process of switching between different PowerShell environments. The next step involves installing PowerShell Core, and you can follow the instructions based on your operating system.

It’s worth noting that PowerShell Core and Windows PowerShell can coexist on the same machine. The Windows/Desktop PowerShell is executed using “powershell.exe,” while PowerShell Core uses “pwsh.exe.”

Once you have the latest version of PowerShell Core installed, you can perform the following steps to switch your session in Visual Studio Code:

  • Click the icon with the PowerShell version number (e.g., [>]5.1) in the lower right-hand corner;
  • From the menu that appears, select the PowerShell Core version you just installed.

You can use these same steps to switch back to other versions of PowerShell if needed. If, for any reason, you do not see the icon in the lower right-hand corner, don’t worry; it should become visible after the next section.

Creating a Hello World Script in Visual Studio Code

In this section, we will walk through the process of creating a simple script to introduce you to some of the fundamentals of Visual Studio Code. In the upcoming parts of this series, we will delve deeper into various features.

Here are the steps to create the script in Visual Studio Code:

Step 1: Open a New File

In Visual Studio Code, you can open a new file using either of these methods:

  • Press CTRL+N;
  • Click on “File” in the top menu and select “New File”

Step 2: Save the File

Once you are in the newly created “Untitled-1” document, save it by using one of these methods:

  • Press CTRL+S;
  • Click on “File” in the top menu and select “Save”

Step 3: Name and Save the File

When prompted to save the file, give it the name “hello.ps1” and choose a location where you’d like to save it.

After completing these steps, you’ll have Visual Studio Code set up with the “hello.ps1” script ready for editing. The script editor will be displayed above, while the Terminal will be located at the bottom of the window.

It’s time to incorporate some code!

function Invoke-HelloWorld {
    param(
        [Parameter()]
        [switch]
        $Backwards,
        
        [Parameter()]
        [switch]
        $TitleCase
    )

    begin {

        $message         = 'This is the "Hello World" example!'
        $helloWorld      = [string]::Empty
        $titleCaseHelper = [System.Globalization.CultureInfo]::new([string]::Empty)

    }

    process {

        if ($Backwards) {
            
            $splitter = -1

            for ($i = 0; $i -lt $message.Split(' ').Count; $i++) {

                $helloWorld += "$($message.Split(' ')[$splitter]) "
                
                $splitter--       

            }            
            
        } else {

            $helloWorld = $message

        }

        if ($TitleCase) {

            $helloWorld = $titleCaseHelper.TextInfo.ToTitleCase($helloWorld.Trim())

        }
    }

    end {

        return $helloWorld.Trim()

    }

}

#Run with no switches specified
Invoke-HelloWorld

#Get the result from using the backwards switch
Invoke-HelloWorld -Backwards

#Get the result from using the title case switch
Invoke-HelloWorld -TitleCase

#Get the result from using the backwards and title case switch'
Invoke-HelloWorld -Backwards -TitleCase

With code at our disposal, we can now explore some of the features of VS Code.

Concealing the Explorer Pane

To toggle the visibility of the explorer pane, you can simply use CTRL+B.

Function Information and Autocompletion

When you enter the following:

Invoke-HelloWorld –

It shows the different parameters available, and their type on the right.

When you start typing a function name, it will also list the available functions.

Invoke-

Executing Code in PowerShell

a side view of coding - colorful words on black screen

 

Let’s dive into executing specific sections of your PowerShell script. While [F5] can run the entire script, you can utilize [F8] to execute a selected portion of your code.

Here’s a step-by-step guide:

  • Highlight the entire function you want to run;
  • Press [F8], which is equivalent to selecting the code you wish to execute, right-clicking, and choosing “Run Selection.”

This action stores the selected function in memory, as evident in the terminal window.

Now, proceed to the section below the function and run some commands that call the function stored in memory.

Highlight “Invoke-HelloWorld” and execute the selection using [F8]. You can also include the comment line just before it.

Your output should resemble the screenshot above, displaying:

This is the “Hello World” example!

Let’s move on to the final example, which involves using both parameter switches:

  • Highlight the example “Invoke-HelloWorld -Backwards -TitleCase” and execute it by pressing [F8].

Running the Entire Script (F5)

To run the entire script, including the option to start debugging if you have set any breakpoints, simply press [F5] or click on “Debug” and then select “Start Debugging.

A Swift Introduction to Debugging

In this quick tutorial, we’ll explore the debugging process using a fresh perspective. To keep things interesting, we’ll set a breakpoint on line 49 (simply click in the empty space to the left of the line number). Line 49 happens to be the return statement within the function:

  • Now, when we engage the [F5] key to initiate debugging, it will pause precisely at this breakpoint, providing us with valuable insights into the script’s current state;
  • On the left-hand side, you’ll find a snapshot of the current status of objects and variables;
  • Along the top, a control bar awaits your commands to navigate through the debugging experience;
  • For our next move, let’s click the upward-pointing arrow, which executes the “Step Out” action;
  • Alternatively, you can employ the keyboard shortcut Shift+F11;
  • The script will halt each time it encounters this point, allowing you to closely inspect the execution until it reaches its conclusion.

Conclusion

Setting up PowerShell in Visual Studio Code is a straightforward process that can significantly boost your productivity as a PowerShell scripter. With features like IntelliSense, integrated terminals, debugging capabilities, and a vast library of extensions, you’ll find yourself writing and managing PowerShell scripts more efficiently than ever before. So, go ahead, install the PowerShell extension, create a new project, and start coding with confidence in Visual Studio Code.