Sitecore PowerShell Common Utilities

Below are some Sitecore PowerShell Common Utilities:

 

# Get a list of all Sitecore users

$lstSitecoreUsers = Get-User -Filter “sitecore\*”

$lstSitecoreUsers | ForEach-Object  {

Write-Host $_.Name

}

 

# Get a list of Admin users only

$lstSitecoreUsers = Get-User -Filter “sitecore\*”

$lstSitecoreUsers | Where{$_.IsAdministrator -eq $true } | ForEach-Object  {

Write-Host $_.Name

}

 

# Get a list of users who are managers

$lstSitecoreUsers = Get-User -Filter “sitecore\*”

$lstSitecoreUsers | Where{$_.Name -like “*manager*” } | ForEach-Object  {

Write-Host $_.Name

}

 

# Get a List of all items under a node – e.g Home

$allItemsUnderHome = Get-Item master: -Query “/sitecore/content/Home//*”

$allItemsUnderHome | ForEach-Object {

Write-Host “Item name: ” + $_.Name

}

 

# Update an Item – e.g. Test Items under Home

$item = Get-Item “master:/sitecore/content/Home/Test”

$item.Editing.BeginEdit();

$item[“Header Title”] = “New title for the test item here”;

$item.Editing.EndEdit();

 

# Get a list of items update in last ‘x’ number of days e.g 30 days

Get-Childitem -Path “master:/sitecore/content/Home”  -recurse |

Sort $_.”__Updated” |

Where-Object   {  $_.”__Updated” -gt (Get-Date).AddDays(-30) }

 

# Show results in a List View – Get a list of items update in last ‘x’ number of days e.g 30 days

get-childitem -Path “master:/sitecore/content/Home” -recurse | `

Sort $_.”__Updated” | `

Where-Object   {  $_.”__Updated” -gt (Get-Date).AddDays(-30) }    |

Show-ListView -property Name, `

@{Label=”Last Updated Date”; Expression={$_.”__Updated”}}, `

@{Label=”Last Updated By”; Expression={ $_.”__Updated By” } } `

-Modal -Width 690 -Height 600 -PageSize 200

Show-Result -Text

 

# Find and Replace – e.g. Find all items under Home with template ‘Generic Page’ and update/replace their ‘Header Subtitle’ with ‘Header title’

Get-Childitem -Path “master:/sitecore/content/Home” -recurse `

| Where-Object { $_.TemplateName -match “Generic Page”  } `

| ForEach-Object { $_.”Header Subtitle” = $_.”Header Title”}

 

# Remove or Create Users, setting user permissions(adding and removing permissions)

$usr =”sitecore\testuser001.admin”;

Remove-User -Identity $usr -ErrorAction SilentlyContinue;

New-User -Identity $usr -Enabled -Password “testpwd001” -Email “testuser001@companymail.com” -FullName “Test User 001” -ErrorAction SilentlyContinue;

Set-User -Identity $usr -IsAdministrator $true;

@(“sitecore\ContentOwner,sitecore\ContentAuthor”).Split(‘,’).Trim()
|
ForEach-Object {

Remove-Rolemember -Identity $_ -Members $usr -ErrorAction SilentlyContinue;

Add-Rolemember -Identity $_ -Members $usr -ErrorAction SilentlyContinue;

}

 

# assign specific security rights to an item
$theHeader = Get-ChildItem -Path “master:/Sitecore/Content/Header” -ErrorAction SilentlyContinue

if($theHeader -ne $null) {
@(“item:create”,”item:write”,”item:delete”,”item:rename”) | ForEach-Object {
$aclRight = $_

$acl1 = New-ItemAcl -AccessRight $aclRight -PropagationType Any -SecurityPermission AllowAccess -Identity “sitecore\Content Owner”
$acl2 = New-ItemAcl -AccessRight $aclRight -PropagationType Any -SecurityPermission AllowAccess -Identity “Content Author”

$theHeader | Add-ItemAcl -AccessRules $acl1,$acl2
}
}

 

# Compare/Count items in Sitecore databases(Master and Web)

$childsMaster = Get-ChildItem master:/sitecore/content/Catalog/Products -Language en-CA -Version *
$childsMaster= $childsMaster | Where { $_.ProductExpireDate -gt (Get-Date) } #| ForEach-Object {$_.ProductExpireDate}
write-host “Count of items Canada Product Catalog(Master): ” $childsMaster.length

$childsWeb = Get-ChildItem web:/sitecore/content/Catalog/Products -Language en-CA -Version *
$childsWeb= $childsWeb | Where { $_.ProductExpireDate -gt (Get-Date) } #| ForEach-Object {$_.ProductExpireDate}
write-host “Count of items Canada Product Catalog(Web): ” $childsWeb.length

#write-host “Compare master Vs Web Databases”
Compare-Object $childsMaster $childsWeb -PassThru

 

# Compare items in different master and Slave Solr

$items_product_index_master = wget “http://solrslave.companysite.com/solr/product_index_master/select?q=product_id%3A*&fq=_language%3Aen-US&rows=9999&fl=product_id&wt=json&indent=true” | ConvertFrom-Json
write-host “Count of items product_index_master: ” $items_product_index_master.response.numfound
#$items_product_index_master.response.docs.product_id

$items_product_index_web = wget “http://solrmsater.companysite.com/solr/product_index_web/select?q=product_id%3A*&fq=_language%3Aen-CA&rows=9999&fl=product_id&wt=json&indent=true” | ConvertFrom-Json
write-host “Count of items product_index_web: ” $items_product_index_web.response.numfound
#$items_product_index_web.response.docs.product_id

#write-host “Compare product_index_master Vs product_index_web”
Compare-Object ($items_product_index_master.response.docs.product_id) ($items_product_index_web.response.docs.product_id)

 

# Compare items in sitecore database and Solr

#write-host “Compare Master Database Vs product_index_web”
Compare-Object ($childsMaster.ProductID) ($items_product_index_web.response.docs.product_id) -PassThru

 

# Remove items or fields of an item/template ( lets say we want to remove Header Subtitle and Footer SubTitle, use Remove-Item
@(
‘master:/sitecore/templates/Rendering Templates/Header/Header SubTitle’,
‘master:/sitecore/templates/Rendering Templates/Footer/Footer SubTitle’
)| ForEach-Object {
$items = Get-Item -Language * -Path $_ -ErrorAction SilentlyContinue
$items | Remove-Item -ErrorAction SilentlyContinue
}

# Remove  specific language versions only of ‘Header Parameters/__Standard Values’ except say English (en), use Remove-ItemLanguage
@(
‘master:/sitecore/templates/Rendering Templates/Header/Header SubTitle/__Standard Values’, ‘master:/sitecore/templates/Rendering Templates/Footer/Footer SubTitle/__Standard Values’
)| ForEach-Object {
$items = Get-Item -Language * -Path $_ -ErrorAction SilentlyContinue
$items | ForEach-Object {
if($_.Language.Name -ne “en”){
$_ |Remove-ItemLanguage -ErrorAction SilentlyContinue
}
}
}

 

….more to continue…

Leave a comment