Skip to content

Instantly share code, notes, and snippets.

@Adamsimsy
Last active April 20, 2023 10:07
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Adamsimsy/30dc60df837c1400e6fccd814b3dc752 to your computer and use it in GitHub Desktop.
Save Adamsimsy/30dc60df837c1400e6fccd814b3dc752 to your computer and use it in GitHub Desktop.
Sitecore Powershell examples
#Use this if you are hitting max items limit and can't change this setting. Warning can be very slow!
function ProcessChildren($path)
{
$childItems = Get-Item master: -Query $path"/*"
$childItems | ForEach-Object {
Write-Host "Item path: " $_.FullPath
ProcessChildren($_.FullPath)
}
}
ProcessChildren("/sitecore/content");
$items = Get-Item master:/content/home//*
$items | ForEach-Object {
$_.Editing.BeginEdit()
$_["Title"] = $_.Fields["Title"].toString() + " changed value";
$_.Editing.EndEdit();
}
$item = Get-Item master:/content/home
$newTemplate = [Sitecore.Configuration.Factory]::GetDatabase("master").Templates["Sample/Sample Item"];
$item.ChangeTemplate($newTemplate)
$dateTime = ([sitecore.dateutil]::IsoDateToDateTime($item["My Date Field"])
Write-Host $dateTime.ToString("dd MMMM yyyy")
#Output 10 September 2016
$item = New-Item "master:/content/home/my new sample item" -type "Sample/Sample Item"
$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;
}
#New-User docs: https://doc.sitecorepowershell.com/appendix/commands/New-User.html
#Include domain in username to specify the target domain (e.g extranet\adam)
New-User -Identity $row."Username" -Enabled -Password $row."Password" -Email $row."Email" -FullName $row."FullName"
}
$item = Get-Item -Path master: -Query "/sitecore/content//*[@@templatename !='Sample Item']"
#Or
$item = Get-Item -Path master: -Query "/sitecore/content//*" | Where-Object { $_.TemplateName -ne "Sample Item" -and $_.TemplateName -ne "Local Datasource Folder" }
$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()
$item = Get-Item master:/content/home
$item.Editing.BeginEdit();
$item["Title"] = "New title for the home item!";
$item.Editing.EndEdit();
$allSitecoreUsers = Get-User -Filter "sitecore\*"
$allSitecoreUsers | ForEach-Object {
Write-Host $_.Name
}
$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";
[Sitecore.Data.Fields.LinkField]$field = $_.Fields["Linked item"]
Write-Host ($field | Format-Table | Out-String)
Get-Item master: -Query "/sitecore/content/home//*" | Show-ListView -Property Name,TemplateName
$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";
}
$everythingUnderHome = Get-Item master: -Query "/sitecore/content/Home//*"
$everythingUnderHome | ForEach-Object {
Write-Host "Item name: " + $_.Name
}
$items = Get-Item master:/content/home//* | Where-Object { $_.Fields["__Final Renderings"] -like "*col-huge*" }
$items | ForEach-Object {
Write-Host $_.Fields["__Final Renderings"]
$_.Editing.BeginEdit()
$_["__Final Renderings"] = $_.Fields["__Final Renderings"].toString().replace("col-huge", "col-wide-1");
$_.Editing.EndEdit();
Write-Host $_.Fields["__Final Renderings"]
}
$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
@michaellwest
Copy link

Sweet.

@hishaamn
Copy link

Great one.

There is a slight error. For the Sitecore Powershell write to csv.ps1, there is a missing . for the Path. Should be Path = $_.ItemPath

@kishanW
Copy link

kishanW commented Aug 15, 2017

Thanks for the examples here. They were very helpful.
I have a question though, is there anyway to access the raw value of a field? As an example, i want to get the raw value of the security field for a given item.

@ankitjoshi
Copy link

This is great!

@onpoc
Copy link

onpoc commented Jul 26, 2018

really helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment