Tip Detecting Sitecore XM Cloud in PowerShell

Created: 2 Jan 2024, last update: 3 Oct 2024

Tip: Detecting Sitecore XM Cloud in PowerShell

How can I determine whether I am in a Sitecore or Sitecore XM Cloud environment within a Sitecore PowerShell script?

[Sitecore.Configuration.About]::VersionInformation() return the version information example "Sitecore XMCloud 1.4.590 (rev. 0)"

Here's a script snippet that demonstrates how to achieve this:

$sitecoreversioninfo = [Sitecore.Configuration.About]::VersionInformation()
if ($sitecoreversioninfo.Contains("XMCloud") -eq $false)
{
    Write-Host "not XM cloud but $sitecoreversioninfo"
} else 
{
     Write-Host "This is $sitecoreversioninfo"
}

How can I retrieve the Sitecore version or the XM Cloud version number in a Sitecore PowerShell script?

$sitecoreversion = [Sitecore.Configuration.About]::Version return the version nummer example "1.4.590.0"

Example of a minimum required script used in https://github.com/jbluemink/Find-overwritten-Sitecore-resource-items:

$sitecoreversion = [Sitecore.Configuration.About]::Version
If ($sitecoreversion.StartsWith('10.1') -or $sitecoreversion.StartsWith('10.2'))
{
 Show-Alert -Title "Sorry this is only for Sitecore 10.3+ and XMCloud"
 exit
}
# note the tool is for Sitecore 10.1+ so older version are not supported,
# I assume the idea is clear and the Sitecore.Configuration.About
# works for all sitecore versions from 6.0 to 10.4+

Summary

By using the Sitecore.Configuration.About class from the Sitecore.Kernel.dll, you can figure out which Sitecore version you are running, including the version number and whether it is XM Cloud or not.

While there are several methods to detect the Sitecore version, such as inspecting configuration files or opening the file \sitecore\shell\sitecore.version.xml, I find the approach described above with the [Sitecore.Configuration.About] class to be the most useful. I employ this functionality in the Find-overwritten-Sitecore-resource-items PowerShell Report for Sitecore IAR items, treating items as resources.