Forked from Adamsimsy/Sitecore Powershell change item template.ps1
Created
August 14, 2017 14:45
-
-
Save kishanW/a9a54ec4cd54891c1b9e784f9dcfd4cc to your computer and use it in GitHub Desktop.
Sitecore Powershell examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$item = Get-Item master:/content/home | |
$newTemplate = [Sitecore.Configuration.Factory]::GetDatabase("master").Templates["Sample/Sample Item"]; | |
$item.ChangeTemplate($newTemplate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$dateTime = ([sitecore.dateutil]::IsoDateToDateTime($item["My Date Field"]) | |
Write-Host $dateTime.ToString("dd MMMM yyyy") | |
#Output 10 September 2016 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$item = New-Item "master:/content/home/my new sample item" -type "Sample/Sample Item" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$xmlWriter = New-Object System.XMl.XmlTextWriter('c:\temp\sitemap.xml',$Null) | |
$xmlWriter.Formatting = 'Indented' | |
$xmlWriter.Indentation = 1 | |
$XmlWriter.IndentChar = "`t" | |
$xmlWriter.WriteStartDocument() | |
$xmlWriter.WriteStartElement('urlset') | |
$XmlWriter.WriteAttributeString('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9') | |
$everythingUnderHome = Get-Item master: -Query "/sitecore/content/Home//*" | |
$baseUrl = "https://example.com" | |
$everythingUnderHome | ForEach-Object { | |
$url = $baseUrl + [Sitecore.Links.LinkManager]::GetItemUrl($_) | |
$xmlWriter.WriteStartElement('url') | |
$xmlWriter.WriteElementString('loc',$url) | |
$xmlWriter.WriteEndElement() | |
} | |
$xmlWriter.WriteEndElement() | |
$xmlWriter.WriteEndDocument() | |
$xmlWriter.Flush() | |
$xmlWriter.Close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$item = Get-Item master:/content/home | |
$item.Editing.BeginEdit(); | |
$item["Title"] = "New title for the home item!"; | |
$item.Editing.EndEdit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$allSitecoreUsers = Get-User -Filter "sitecore\*" | |
$allSitecoreUsers | ForEach-Object { | |
Write-Host $_.Name | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$item = Get-Item master:/content/home/old/item | |
#Will move and place this item under the target new | |
Move-Item -Path $item.ItemPath "master:\sitecore\content\Home\new"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Sitecore.Data.Fields.LinkField]$field = $_.Fields["Linked item"] | |
Write-Host ($field | Format-Table | Out-String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csv = Import-Csv "C:\temp\my-csv-file.csv" | |
foreach($row in $csv) | |
{ | |
if ($row -eq $csv[0]) | |
{ | |
#Skip the first row as it contains the column headings in your CSV | |
continue; | |
} | |
#Output value for Column1 and Column2 | |
Write-Host $row."Column1"; | |
Write-Host $row."Column2"; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$everythingUnderHome = Get-Item master: -Query "/sitecore/content/Home//*" | |
$everythingUnderHome | ForEach-Object { | |
Write-Host "Item name: " + $_.Name | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Get an alias | |
$alias = = Get-Item -Path "master:/sitecore/system/Aliases/test" | |
#Get the link field | |
[Sitecore.Data.Fields.LinkField]$field = $_.Fields["Linked item"] | |
# the path to the target item | |
$field.InternalPath | |
# The guid if internal. Otherwise this is an Empty GUID | |
$field.TargetID | |
# 'internal' or 'external' | |
$field.LinkType | |
# Other proporties include MediaPath, QueryString and Target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$everythingUnderHome = Get-Item master: -Query "/sitecore/content/Home//*" | |
$outputFilePath = "C:\temp\my-csv-file.csv" | |
$results = @(); | |
$everythingUnderHome | ForEach-Object { | |
$properties = @{ | |
Name = $_.Name | |
Template = $_.TemplateName | |
Path = $_ItemPath | |
} | |
$results += New-Object psobject -Property $properties | |
} | |
$Results | Select-Object Name,Template,Path | Export-Csv -notypeinformation -Path $outputFilePath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment