protected override void ExecuteCmdlet() { var serverRelativeWebUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl); if (!ServerRelativePageUrl.ToLowerInvariant().StartsWith(serverRelativeWebUrl.ToLowerInvariant())) { ServerRelativePageUrl = UrlUtility.Combine(serverRelativeWebUrl, ServerRelativePageUrl); } Guid id; if (Identity.Id == Guid.Empty) { var wp = CurrentWeb.GetWebParts(ServerRelativePageUrl).FirstOrDefault(wps => wps.WebPart.Title == Identity.Title); if (wp != null) { id = wp.Id; } else { throw new Exception($"Web Part with title '{Identity.Title}' cannot be found on page with URL {ServerRelativePageUrl}"); } } else { id = Identity.Id; } WriteObject(CurrentWeb.GetWebPartXml(id, ServerRelativePageUrl)); }