Let's Share

My personal place to share knowledge about Sitecore, Powershell, Amazon Web Services and .NET

Let's explore Sitecore users with Powershell

posted by Robert Senktas   | 03-02-2017


Part 1 - Export custom user properties

image Problem: Export all custom user properties from old site to new site.
Solution: The Powershell Script.
This example shows how to get all enabled (active) users and theirs custom properties and save them to the CSV file. If is not neccessary to export some custom properties then should be removed from properties array.




Explore Sitecore user properties

Sitecore Powershell Extension provides few commands to get/set Sitecore users: Get-User, Set-User and New-User.
I created a test user 'sample@user.com' in 'extranet' domain. When I run the command:

Get-User -Identity "extranet\sampleuser"

I received the following results:

Name                     Domain       IsAdministrator IsAuthenticated
----                     ------       --------------- ---------------
extranet\sampleuser      extranet     False           False

Nothin interesting, right? Let's explore a little bit user object with command Get-Member:

Get-User -Identity "extranet\sampleuser" | Get-Member

image

Now there is a muche more information. There are interesting properties like Profile, Identity, Roles. My goal is an user migration with a profile data, then I will try explore the Profile property:

$user.Profile | Get-Member
image

Sitecore users could have defined a custom properties. How to define a custom properties is explained in Sitecore documentation. Now I will show simple code to display all custom properties and their values. Note: If custom property is empty will not be displayed. That's why I set '-' as property value.

$customProperties = $user.Profile.GetCustomPropertyNames()
foreach( $property in $customProperties)
{
   $property + " : " + $user.Profile.GetCustomProperty($property)
}

Script execution resuls are shown on picture below:

image

As most of smart guys I'm lazy :) And I wrote simple script to generate properties array, becasue I prefer automation over manual work:

foreach( $property in $properties)
{
  "@{Name='$property';Expression={ `$PSItem.Profile.GetCustomProperty('$property') }},"     
}

Here are results of clever script:

@{Name='Email';Expression={ $PSItem.Profile.GetCustomProperty('Email') }},
@{Name='FirstName';Expression={ $PSItem.Profile.GetCustomProperty('FirstName') }},
@{Name='LastName';Expression={ $PSItem.Profile.GetCustomProperty('LastName') }},
@{Name='Title';Expression={ $PSItem.Profile.GetCustomProperty('Title') }},
@{Name='Company';Expression={ $PSItem.Profile.GetCustomProperty('Company') }},
@{Name='Country';Expression={ $PSItem.Profile.GetCustomProperty('Country') }},
@{Name='ZipCode';Expression={ $PSItem.Profile.GetCustomProperty('ZipCode') }},
@{Name='Department';Expression={ $PSItem.Profile.GetCustomProperty('Department') }},
@{Name='Street';Expression={ $PSItem.Profile.GetCustomProperty('Street') }},
@{Name='City';Expression={ $PSItem.Profile.GetCustomProperty('City') }},
@{Name='Phone';Expression={ $PSItem.Profile.GetCustomProperty('Phone') }},
@{Name='Username';Expression={ $PSItem.Profile.GetCustomProperty('Username') }},

In next part I will show you how to import properties to new solution. See you soon!

Sitecore Powershell Users Export
Comments
This is a personal blog. All opinions here are my own opinions and do not represent my employer’s view in anyway.

Sitecore Automation Module