Category Archives: Sitecore with Powershell

Exporting the User’s custom properties from Sitecore using PowerShell

In our requirement, the client needs to Export all custom user properties from the old site to the new site as they planned to migrate from the new environment. In this case, writing C# or other language code to retrieve the User information is time taking process so we decided to make this with help of Powershell. So in this article, we are going to explain with the simple Powershell Script.

This example shows how to get all active/Inactive users and theirs custom properties based on domain, post fetching the data we planned to save them in the CSV file which will be easy to share. It is not necessary to export some custom properties then should be removed from properties.

Note: Sitecore user profile custom properties are case sensitive

STEP #1: Get user details using Get-User cmdlet with condition parameter

From the below code, we are fetching the user information based on the “addotnetAdmin/” domain. Once it’s executed, the user variable will have all the users who match the domain “addotnetAdmin/”.

$users = Get-User -Filter * | Where-Object {$_.Domain -eq “addotnetAdmin”}

STEP #2: Create Array Property with help of $PSItem.Profile.GetCustomProperty() method and assing the CustomProperty names.

The pipeline variable ($_ or $PSItem) represents each object upon processing it.

$property = @(

@{Name=”Email Id”;Expression={$PSItem.Profile.GetCustomProperty(“EmailAddress”)}},
@{Name=”First Name”;Expression={$PSItem.Profile.GetCustomProperty(“FirstName”)}},
@{Name=”Last Name”;Expression={$PSItem.Profile.GetCustomProperty(“LastName”)}},
@{Name=”SocialSecurity”;Expression={$PSItem.Profile.GetCustomProperty(“SocialSecurity”)}},
@{Name=”Zipcode”;Expression={$PSItem.Profile.GetCustomProperty(“Zipcode”)}},
@{Name=”Profession”;Expression={$PSItem.Profile.GetCustomProperty(“Profession”)}},

)

STEP #3: Select Custom Property value from $user list and export to CSV.

$users | Select-Object -Property $property| Export-CSV -Path C:\dotnet-helpers\ExportUser.csv -Append -NoTypeInformation

Full Code:

$users = Get-User -Filter * | Where-Object {$_.Domain -eq "addotnetAdmin"}

$property = @(
@{Name="Email Id";Expression={$PSItem.Profile.GetCustomProperty("EmailAddress")}},
@{Name="First Name";Expression={$PSItem.Profile.GetCustomProperty("FirstName")}},
@{Name="Last Name";Expression={$PSItem.Profile.GetCustomProperty("LastName")}},
@{Name="SocialSecurity";Expression={$PSItem.Profile.GetCustomProperty("SocialSecurity")}},
@{Name="Zipcode";Expression={$PSItem.Profile.GetCustomProperty("Zipcode")}},
@{Name="Profession";Expression={$PSItem.Profile.GetCustomProperty("Profession")}},
)

$users | Select-Object -Property $property| Export-CSV -Path C:\Utility\Out.csv -Append -NoTypeInformation

OUTPUT

How to get Sitecore Admin users for the domain using Sitecore PowerShell Extensions

The revisiting of Admin access is an important activity for the support team as most of the time we may provide temporary access for a certain time and forgot to roll back.

In this post, we will explore a great way of extracting Admin user data . This is the simplest way to extract using the Sitecore Powershell ISE.

Sample 1: Get All User mapped with specific domain

##################################################
#Project: Get a list of all users for Domain
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 & SiteCore 8.1
#E-Mail: mail2thiyaguji@gmail.com 
##################################################
#Getting User list by filtering with domain "dotnet-helpers"
$allSitecoreUsers = Get-User -Filter “dotnet-helpers\*”

$allSitecoreUsers | ForEach-Object  {
Write-Host $_.Name
}

Sample 2: Get All Admin Users mapped with specific domain

#####################################################################################################
#Project: How to get Sitecore Admin users for the domain using Sitecore PowerShell Extensions.
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 & SiteCore 8.1
#E-Mail: mail2thiyaguji@gmail.com 
#####################################################################################################
 
$adminSitecoreUsers = Get-User -Filter “dotnet-helpers\*”

$adminSitecoreUsers | Where{$_.IsAdministrator -eq $true } | ForEach-Object  {
Write-Host $_.Name
}

Output

Unlocking all locked items in Sitecore using Sitecore PowerShell Extensions (SPE)

In my Sitecore environment, there are 100+ non-admin users are working and we have been asked many times if it is possible for non admin users in Sitecore to be able to unlock items that have been locked by others Users. But in open, we dont have option for non-admin users to Un-Lock the others locked items, in this case they need to contact the specific person or admin users for Un-Locking items.

But this is a feature that many user want to perform Un-locking without others support, So based on requirement we thought to write custom command to unlock the items with help of Sitecore Powershell extension.

In this article, we going to see how to create custom Powershell script for Un-Locking the parent and child items for all the language versions.

STEP 1:

First you need to Get Source , child path items and assigned to the variables ($rootItem, $ChildItems ). After below script execution, the variable $items will contain the parent and its child items collection.

$sourcePath = “/sitecore/content/Sites/White/home/components”
$rootItem = Get-Item -Path $sourcePath
$ChildItems = Get-ChildItem -Path $sourcePath -Recurse
$items = $ChildItems + $rootItem

STEP 2:

Next you need to start looping the items ($items) one by one to check for all the languages.

foreach ($item in $items)
{
foreach ($version in $item.Versions.GetVersions($true))
{
Your unlocking login here….
}
}

STEP 3:

The main logic is to check each item with each language version to verify whether its locked or not. If its locked then below condition will allow to Un-Lock the specific item on specific language version.

if($version.Locking.IsLocked())
{
$version.Editing.BeginEdit();
$version.Locking.Unlock();
$version.Editing.EndEdit();
Write-Host “Item Un-locked” $item.ID “for Language” $version.Language;
}

Full code

###########################################################################################
#Project : Unlocking all locked items in Sitecore using Sitecore PowerShell Extensions (SPE)
#Developer : Thiyagu S (dotnet-helpers.com)
#Tested Tool : Sitecore 8.1 
#E-Mail : mail2thiyaguji@gmail.com 
###########################################################################################

$sourcePath = "/sitecore/content/Sites/White/home/components"

function UnlockSiteCoreItems
{
    $rootItem = Get-Item -Path $sourcePath
    $ChildItemsToUnlock = Get-ChildItem -Path $sourcePath -Recurse
    $items = $ChildItemsToUnlock + $rootItem
    
    foreach ($item in $items)
    {
        foreach ($version in $item.Versions.GetVersions($true))
        {
            if($version.Locking.IsLocked())
            {
                $version.Editing.BeginEdit();
                $version.Locking.Unlock();
                $version.Editing.EndEdit();
                Write-Host "Item Un-locked" $item.ID "for Language" $version.Language;
            }
        }
    }
}
$unlockedItemsDetails = UnlockSiteCoreItems

OUTPUT: