How to debug PowerShell Script with Visual Studio Code

In this PowerShell SharePoint, I would like to enlighten you PowerShell scripting skills by adding an extra muscle “Debugging PowerShell with Visual Studio Code” skills. Here we will discuss how to debug PowerShell script using visual studio code.

I am sure most of us have got expertise in writing PowerShell scripts for various requirements. While writing any code block we feel easy if we have to debug facility/feature.

All this while we might be adding many “Write-Host” commands to validate where the code is getting failed. Let us say goodbye for that legacy process and thank you to the “Microsoft Visual Studio Code” IDE.

I believe most of you have got a chance to work with VS Code IDE if not please start downloading it from https://code.visualstudio.com/download. This is a free S/W from Microsoft, which is really powerful with default plugins for intelligence while writing the scripts in JS, NODE JS, AngulerJS and PowerShell.

Debugging PowerShell Script with Visual Studio Code

Now, let us learn how to debug/execute a PowerShell Script from Visual Studio Code IDE.

As soon as you install the Visual Studio Code IDE, click on Color Theme as part of settings (at the left bottom corner of the IDE)

debug powershell script in visual studio

Select “PowerShell ISE” as part of the dropdown list as highlighted below:

debug powershell script in visual studio code

Once install the ISE, go to settings to add a new change in setting.json file (you can go to the setting by clicking on the setting, which you can see as part of step1 screenshot)

Add the below line to change the VS Code IDE color theme (optional)

“workbench.colorTheme”: “PowerShell ISE”

Once you install this PowerShell ISE, VS Code IDE will get the ability to show you the intelligence while writing the PowerShell Script like as below.

debug powershell visual studio code

Now create a new PowerShell Script by clicking “ctrl+N

Copy and paste the below script to get the provided file information.

function Get-ItemInfo($filePath)
{
$itemProperties = Get-ChildItem -Path $filePath | Select-Object Name,FullName,CreationTime,LastWriteTime
$owner = (Get-Acl -Path $filePath).Owner
$result = [PSCustomObject]@{
Name = $itemProperties.Name
FullName = $itemProperties.FullName
CreationTime = $itemProperties.CreationTime
ModifiedDate = $itemProperties.LastWriteTime
Owner = $owner
}
return $result
}
Get-ItemInfo "c:\Krishna\PowerShell Scripts\Get-fileInfo.ps1"

Save the script as “Get-fileInfo.ps1”

Now you should be able to see as below:

debugging powershell script in visual studio code

You can see an option to debug the code “highlighted in the above screenshot”.

Click on the debug icon to see the various options like variables, watch and Call Stack.

Like the way how we debug the code in VS IDE select the desired line of code to start the debugging. By clicking on the number on the left side of the line (as shown in the below screenshot).

how to debug powershell script in vscode

Now you can see that I have added the debug pointer on line 4

Click on a green play button, the IDE will start the debugging and shows values of variables on the left-hand side.

how to debug powershell script in visual studio code

Click on F10 to proceed further to debug the code or hit F5 to complete the debugging.

Once you complete the execution the output of the script is as follows:

how to debug powershell script using visual studio code

You may like the following PowerShell SharePoint tutorials:

I hope this PowerShell tutorial, we discussed how to debug PowerShell script using Visual Studio code.

>