Debugging PowerShell Script with Visual Studio Code

visual studio code powershell
InfoPath alternatives for form designing SharePoint

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

I am sure most of us has got expertise in writing PowerShell scripts for various requirements. While writing any code block we feel easy if we have debugging facility / feature. All these while we might be adding many “Write-Host” commands to validate where the code is getting failed. Let us say good bye for that legacy process and thank you to “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.

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

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


2. Select “PowerShell ISE” as part of dropdown list as highlighted below


visual studio code powershell
visual studio code powershell

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

Read some SharePoint Online tutorial:

4. Add the below line to change the VS Code IDE color theme (optional)
“workbench.colorTheme”: “PowerShell ISE”
5. Once you install this ISE , VS Code IDE will get the ability to show you the intelligence while writing the PowerShell Script like as below.

visual studio code powershell debug
visual studio code powershell debug

6. Now create a new PowerShell Script by clicking “ctrl+N”
7. 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”

8. Save the script as “Get-fileInfo.ps1”
9. Now you should be able to see as below

debug powershell visual studio code
debug powershell visual studio code





10. You can see an option to debug the code “highlighted in the above screen shot”
11. Click on the debug icon to see the various options like variables, watch and Call Stack
12. 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 screen shot).

visual studio code powershell console
visual studio code powershell console

13. Now you can see that I have added the debug pointer on line 4
14. Click on green play button, the IDE will start the debugging and shows values of variables on left hand side

how to debug powershell in visual studio code
how to debug powershell in visual studio code

15. Click on F10 to proceed further to debug the code or hit F5 to complete the debugging
16. Once you complete the execution the output of the script is as follows

how to debug powershell script using visual studio code
how to debug powershell script using visual studio code

Hope this is helpful and happy scripting in PowerShell.

Similar SharePoint 2013 Tutorials

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →

Leave a Reply