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)
Select “PowerShell ISE” as part of the dropdown list as highlighted below:
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.
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:
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).
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.
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:
You may like the following PowerShell SharePoint tutorials:
- Delete content type from SharePoint list using PowerShell
- How to activate publishing feature in SharePoint 2013/2016 using PowerShell Script
- 3 Different ways to Change Site Collection URL in SharePoint 2013/2016 using PowerShell
- PowerShell Script to deploy WSP Solution in SharePoint 2013
- How to change background color in visual studio code
- What is a Visual Studio Code Workspace
- Create a console application in visual studio code
I hope this PowerShell tutorial, we discussed how to debug PowerShell script using Visual Studio code.
I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ 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