.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Create HTML Report of Facebook Urls using PowerShell

Posted By:Jean Paul       Posted Date: September 27, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we can explore a scenario of adding custom User Profile Property and generating a report based on it. The challenge here is that the report generation should be done with PowerShell as it is easier to create, execute and manage for an Administrator.

In this article we can explore a scenario of adding custom User Profile Property and generating a report based on it.  The challenge here is that the report generation should be done with PowerShell as it is easier to create, execute and manage for an Administrator.


For each of the user in the company requires a property to capture their Facebook Url.  This property can be edited through the My Profile page by the respective user.

An HTML report has to be generated for the all users with the url value assigned.  The report should be in PowerShell so that Administrators can modify it.  The report should look like:


As each user needs to be assigned with a custom property we have to use the User Profile Service Application.
The implementation steps are following:
1.  Create a User Profile Property
2.  Update Users with Facebook Url
3.  Create PowerShell Script
4.  Generate Report

User Profile Property

As the first step we need to create a new User Profile Property.  SharePoint 2010 provides User Profile Properties like:
·  Account Name
·  Email
·  Web site
·  Hire date etc.

But these properties may not be enough for your Organizational Needs!
We can create Custom User Profile Property to address the needs.  In this particular scenario we need to add a property of type string to store Facebook Url.
Note: The User Profile Service Application is the service application responsible for storing the User Profiles, Synchronization, My Site Configuration etc.

In order to create custom user profile property, open Central Administration > Manage service applications > User Profile Service Application > Manage. 

In the appearing page you can see People, Synchronization, Organization, My Site Settings groups.  From the People group select Manage User Properties.

Manage User Properties

This page displays the existing User Profile Properties.  You can see the properties listed as shown below:

Using this screen we can Add / Edit / Delete properties.  Click the New Property link from the top for adding the Facebook Url property. 
In the appearing page enter the details as shown below:

Please note that the Name represents programmatic purpose and Display Name represents formatted display purpose.  Enter the Type as string and Length as 255. 

We are expecting the following values for the Facebook Url property:

 setting the Property values click the OK button to save changes.  Now our new property should be get listed in the Properties page > Custom Properties section.

Manage User Profiles

Now we need to set the value for this property.  The value can be set through:
·  My Site > My Profile page of each user
·  Central Administration by Administrator

We can quickly set the profile properties through Central Administration > Manage service applications > User Profile Service Application > Manage.

In the appearing page click the Manage User Profiles link as highlighted above.  You will get the following page:

Enter the user name in the text box and click Find button.  For the results retrieved, choose the Edit My Profile context menu to set the Facebook Url property.

Save the changes and repeat this for a couple of Profiles.
Now we are ready with enough Profiles assigned with Facebook Url property.

Create PowerShell Script

We can start with creating the PowerShell script.  For writing the script we can use the editor included with Windows.

The script should contain the following activities:
1.  Enumerate all the User Profiles
2.  Write the Account Name, Facebook Url to Screen
3.  Write  the Account Name, Facebook Url to HTML File

Following is the script which achieves the same:

$RootSite = "http://localhost";
$site = new-object Microsoft.SharePoint.SPSite($RootSite);
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site); 
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()

$array = @()

write-output "Account Name, Facebook Url"

foreach($profile in $AllProfiles)
  $FacebookUrl = $profile["FacebookUrl"].value
  $AccountName = $profile["AccountName"].value
  $IsEmpty = [string]::IsNullOrEmpty($FacebookUrl)
  if ($IsEmpty -ne "False")
  # Create object and store in array
  $obj = New-Object System.Object
  $obj | Add-Member -type NoteProperty -name "Account Name" -value "$AccountName"
  $obj | Add-Member -type NoteProperty -name "Facebook Url" -value "$FacebookUrl"
  $array += $obj
  # Output to Screen
  write-output "$AccountName, $FacebookUrl"

# Output to HTML, sorted
$array | Select-Object | Sort-Object -Descending -Property "FacebookUrl" | ConvertTo-Html -title "Active Users" | Set-Content .\FacebookUsers.html


The code is explained below:

The first 3 lines represent:
o  Adding namespaces
The next 5 line represents creation of SPSite, UserProfileManager Instances:
o  new-object keyword represents new operator in C#
o  $ symbol is used to create variables
o  GetEnumerator() is invoked on ProfileManager instance
The foreach loops over the $AllProfiles variable and inside it:
o  Profiles with Facebook Url empty are discarded
o  New object is created with properties Account Name, Facebook Url
o  Values are assigned to the new object
o  Object is added to the Array
o  Writes the row to SCREEN (Writing to HTML follows the foreach loop)
Please note that the following statement adds object to the array:
o  $array += $obj
The write-output statement:
o  Writes the text to the Output screen (ISE Editor)
Note: Under the hood, the script is interpreted and .Net Reflection is used to invoke the methods & properties.  For example new-object maps to new in C# and invokes Activator.CreateInstance method of Reflection.


The comparison operators available in PowerShell are given below:

Operator Description
-eq Equal to
-lt Less than
-gt Greater than
-ge Greater than or Equal to
-le Less than or equal to
-ne Not equal to
-like Like (* for wildcard)
-contains Contains (used for collections)

More operators can be found in the References section.

Comments in PowerShell

# is used to represent a single line comment

Arrays in PowerShell

The array is created using @ symbol
$arrayVariable = @()

Objects in PowerShell

A class is created and the properties are assigned using code below:
  $obj = New-Object System.Object
    $obj | Add-Member -type NoteProperty -name "Account Name" -value "$AccountName"
    $obj | Add-Member -type NoteProperty -name "Facebook Url" -value "$FacebookUrl"
The -name tag specifies the property name of object.  The -value tag specifies the property value of object.

Formatted HTML Output in PowerShell

Creating an HTML file includes adding HTML tags like <table>, <tr>, <td> etc.  PowerShell Cmdlet named ConvertTo-HTML helps us in performing this job.

Executing Code

You can execute the code in ISE Editor and view the result on screen.

The HTML file generated will be in the folder highlighted above and varies from machine to machine.  On opening the file in Internet Explorer you can view the following results.

This concludes our article on HTML report generation using PowerShell.




In this article we have explored a scenario of creating custom User Profile Property & HTML Report generation using PowerShell.  To summarize with, following are the aspects we have explored:
·  User Profile Service Application
·  Creating custom User Profile Property
·  PowerShell Comparison Operators
·  Using PowerShell for HTML Report Generation
The source code attachment contains the script we have discussed.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend