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