-1

I want to rename items with specific name in my Sitecore content tree using PowerShell script to a new name. I tried the following but no change-

Set-Location -Path master:\content\HyperLocal\Arena\

$itemcount = 0

Get-ChildItem -Recurse | ForEach-Object {
    $rendering = Get-Rendering -Item $_
    if((!([string]:: IsNullOrEmpty($rendering))) -and $_.Name.Contains("renamemeabhishek"))
    {
        $itemcount = $itemcount + 1
        $NewName = $_.Name.Replace("renamemeabhishek", "iamrenamed") 
        $ItemPathOnly = $_.FullPath.Substring(0, $_.FullPath.LastIndexOf("/")) 
        $NewItemPath = Join-Path $ItemPathOnly -ChildPath $NewName 
        $oldName = Join-Path $ItemPathOnly -ChildPath $_.Name


        Move-Item $oldName $NewItemPath 
        Write-Host "$OldName renamed to $NewItemPath"
        if ((!([string]::$_."__Display Name")) -and $_."__Display Name".Contains("renamemeabhishek"))
        {
            $_."__Display Name" = $_."__Display Name".Replace("renamemeabhishek","iamrenamed")
        }
        # - This line below resets the display name to the item name, in essence 
        # - nullifying the Display Name property 
        #Reset-ItemField -Item $ _ -IncludeStandardFields -Name "_Display Name"
    }
}

Write-Host "Task Completed - Item Count: " $itemcount

I want to rename this "renamemeabhishek" item under following path to a new name or item with similar name to the new name given. Please suggest.

2

2 Answers 2

1

Rather than moving the item, you could edit the item instead.

Also, if you split the task into two separate functions 1) Finding Items, 2) Renaming Items, you can test the first function by outputting the results to a table.

If you are happy with the proposed items that will be changed, then update the for loop to call the second function (see commented line).

$sourcePath = Get-Item "/sitecore/content/HyperLocal/Arena";

function Find-Items {
    $items = Get-ChildItem -Path $sourcePath.Paths.FullPath | 
    Where-Object {($_.Name.Contains("iamrenamed")) }
        
    foreach($item in $items) {
        ## Uncomment line below to execute the rename
        ## RenameItem($item)
        $item
    }
}
function RenameItem($item)
{
    $name = $item.Name
    $newName = $name.Replace("iamrenamed","renamemeabhishek")

    $ItemPathOnly = $item.FullPath.Substring(0, $item.FullPath.LastIndexOf("/")) 
    
    $destinationPath = "master:$ItemPathOnly/$newName"
    
    if(Test-Path -Path $destinationPath)
    {
        Write-Host "master:$ItemPathOnly/$name exist already - cant rename";
    }
    else
    {
        $item.Editing.BeginEdit()
        $item.Name = $newName
        $item.Editing.EndEdit()
        Write-Host "Item renamed to $destinationPath";
        return $item;
    } 
}


$props = @{
    InfoTitle = "Media being renamed"
    InfoDescription = "Lists all media items that the script will eventually rename"
    PageSize = 25
}

 Find-Items | Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
        @{Label="Updated"; Expression={$_.__Updated} },
        @{Label="Updated by"; Expression={$_."__Updated by"} },
        @{Label="Created"; Expression={$_.__Created} },
        @{Label="Created by"; Expression={$_."__Created by"} },
        @{Label="Path"; Expression={$_.ItemPath} }
        
Close-Window
0

I think you have used the script from following link - https://sabor413blog.wordpress.com/2018/01/26/using-powershell-to-rename-items-in-sitecore/

I think there is no need to perform Move-Item to rename an item. Instead you can use below inside your if logic and it shall work. You can add Write-Host to it as per your convenience -

if($item.Name.Contains("oldname")){
$item.Editing.BeginEdit()
$item.Name = $item.Name.Replace('oldname','newname')
$item.Editing.EndEdit()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.