Write to ULS log using PowerShell in SharePoint 2013

Write to ULS log using PowerShell in SharePoint 2013
SharePoint deveopment training course

In this post, we will discuss how we can write to SharePoint 2013 uls logs using PowerShell in SharePoint 2013. SharePoint maintains Logs and Exceptions of SharePoint in ULS (Unified Logging Service). By default SharePoint stored in .log files in 15 hive folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\LOGS). But you can change the default path to some other location.

In real-world scenarios to get details from ULS logs is difficult without using a tool, You can use SharePoint LogViewer to get logs details in SharePoint 2013. In most of the times, SharePoint throghs a Correlation ID instead of the real error details. By using PowerShell you can retrieve original error messages based on correlation id. You can read: Get Error Details based on Correlation ID from ULS Logs in SharePoint 2013 using PowerShell

We can also write our custom messages to ULS logs using PowerShell or SharePoint server object model.

Below is the PowerShell code to write to ULS logs. We can put the below code in Windows PowerShell ISE

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$diagSvc = [Microsoft.SharePoint.Administration.SPDiagnosticsService]::Local

$category = new-object Microsoft.SharePoint.Administration.SPDiagnosticsCategory(“My Custom Category”,


[Microsoft.SharePoint.Administration.EventSeverity]::Error )

$diagSvc.WriteTrace(0, $category, [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable, “This is our custom error message writted from PowerShell” )

After this if you want to check in ULS logs file you can see it will write like below:

05/10/2016 12:22:14.58 PowerShell_ISE.exe (0x9598) 0x65C4 Unknown My Custom Category 00000 Monitorable This is our custom error message writted from PowerShell cf54b83a-a9d2-0006-4a21-56cfd2a9d101

Write to ULS log using PowerShell in SharePoint 2013

Write to ULS log using PowerShell in SharePoint 2013Write to ULS logs using SharePoint server object model:
You can also use SharePoint 2013 server side object model code to write to uls log. You can use the below code in a visual web part.

SPDiagnosticsCategory category = new SPDiagnosticsCategory(“My Custom Category”,TraceSeverity.High,EventSeverity.ErrorCritical);

SPDiagnosticsService.Local.WriteTrace(0,category,TraceSeverity.High,”This is our custom error message writted from SharePoint 2013 Server object model”);

You may like following SharePoint tutorials:

Hope this will be helpful to Write to ULS log using PowerShell in SharePoint 2013/2016.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →