Sitecore PowerShell - Helpful scripts for developers


Sitecore Powershell module is basically one of the powerful module in Sitecore. Using Powershell scripts, it makes our life simple. If you want to learn more about Sitecore Powershell. Please refer below links to learn more:

Sitecore Powershell Document

I have a few Scenarios for which I have used PowerShell scripts to get the results.

Get the list of items not have item-level fallback enabled

Here I'm trying to fetch the list of items and their paths not having item-level fallback not enabled. I gave a source path where it will read that item as well as the children under that folder as well and provide the list of path to verify:

$sourcePath = "master:/sitecore/content/Home"
$items = Get-ChildItem -Path $sourcePath -Recurse
$rootItem = Get-Item -Path $sourcePath
$items = $items + $rootItem

foreach($item in $items){
    if ($item.Fields["__Enable Item Fallback"].Value -ne "1")
    {
        Write-Host $item.ID $item.Fields["__Enable Item Fallback"].Value $item.Paths.Path
    }

}


Item Fallback

Powershell script to enable item-level fallback

We have already got the list of items for which item-level fallback is not enabled. Now with the below script, we directly update the items for the field "__Enable Item Fallback" value to 1 (Checked/ Enable).

$sourcePath = "master:/sitecore/content/Home"
$items = Get-ChildItem -Path $sourcePath -Recurse
$rootItem = Get-Item -Path $sourcePath
$items = $items + $rootItem

foreach($item in $items){
    if ($item.Fields["__Enable Item Fallback"].Value -ne "1")
    {
        $item.Editing.BeginEdit();
        $item.Fields["__Enable Item Fallback"].Value = '1'
        $item.Editing.EndEdit();
        Write-Host $item.ID $item.Fields["__Enable Item Fallback"].Value $item.Paths.Path
    }
}

Get list of items without having a specific language version

In case of multilingual website, there are some scenarios where you may need to pull items that are not having a specific language version. Here in the below script, I'm pulling items that are not having "en" version of items.

$sourcePath =  "master:/sitecore/content/US/Home"
$items = Get-ChildItem -Path $sourcePath -Recurse
$rootItem = Get-Item -Path $sourcePath
$items = $items + $rootItem

    foreach ($item in $items)
    {     
         if($item.Versions.GetVersions($true).Count -eq 0)
         {
              Write-Host "No Version: "$item.Paths.FullPath
         }
        $valid = $true
        foreach ($version in $item.Versions.GetVersions($true))
        {            
            if ($version.Language -eq "en")
            {
                $valid = $false                
            }            
        }
        if($valid -eq $true)
        {
           Write-Host "No English Version: "$item.Paths.FullPath
        }         
    }

Powershell script to get the size of media items

To know the size of media items like images, pdfs, videos, etc., under a media library. Here is the script to find the size of media items in MB under a specific item path.

[double]$count = 0
$items = gci -path "/sitecore/media libray/Images"
foreach($item in $items)
{
$size = ($item.Fields["Size"].Value -as [double])/1024/1024
$count = $count + $size
}

Write-Host "Total size = " $count "MB"


Please change highlighted as per the need.

Comments