Last week, a customer asked me how to find all user profiles in SharePoint that did not have an entry for a specific property, in particular, a custom one by him.
So, I started searching on the net by some related subjects and I found this post, almost exactly the same as I needed. Here, I would like to thank Steve Mann to have shared it with us! Here is the original link: http://stevemannspath.blogspot.com.br/2013/05/sharepoint-20102013-using-powershell-to.html
Originally, the script try to find all people that did not have a picture associated with their SharePoint User Profile. A user profile’s picture information is stored in the “PictureURL” property.
However, my client needs to find by a custom property called “Pais” (Pais = Country in Portuguese), so, the script below will find all profiles that do not have an entry in “Pais” and will print all of them on the screen (the original part of the script) and after that, it will export to a .CSV file – this is my touch :-)
Of course that we must perform some adjust to run it with success, so, these are the steps:
1) Copy the script below and save it as “GetUsersWithoutProperty.ps1”:
#*********************************************************************************#
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# Dynamic Settings
$mySiteUrl = “http://mysites.mabotega.local/” #Change for your mysites url
$outputFile = “C:\UserProfiles.csv” #Choose the name that you want for the .csv file
#$findProperty = “PictureUrl” #This is the original variable
$findProperty = “Pais” #This is the variable that I will use
Write-Host “Beginning Processing–`n”
# Obtain Context based on site
$mySiteHostSite = Get-SPSite $mySiteUrl
$mySiteHostWeb = $mySiteHostSite.OpenWeb()
$context = Get-SPServiceContext $mySiteHostSite
# Obtain Profiles from the Profile Manager
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$AllProfiles = $profileManager.GetEnumerator()
$outputCollection = @()
# Loop through profiles and retrieve the desired property
foreach ($profile in $AllProfiles)
{
$output = New-Object System.Object
$output | Add-Member -type NoteProperty -Name AccountName -Value $profile["AccountName"].ToString()
$output | Add-Member -type NoteProperty -Name $findProperty -Value $profile[$findProperty]
$outputCollection += $output
}
# List all Accounts that do not contain the property
$outputCollection | Where-Object {[bool]$_.($findProperty) -ne $true}
Write-Host “Exporting profiles”
$outputCollection | Export-Csv $outputFile –NoTypeInformation
#*********************************************************************************#
2) Open the “SharePoint Management Shell” as administrator:
Note: In my scenario, I am using my SharePoint Setup account: RBTSHPSR01;
3) Navigate until the folder that you saved the script above;
4) Run the script typing in the SharePoint Management Shell prompt: .\GetUsersWithoutProperty.ps1
5) For the first time, I received the error message:
6) To solve it, go to “Manage Service Applications” menu under the “Service Application” section:
7) Just select the “User Profile Service Application” configured in your farm and after that, click on the “Permission” button:
8) Be sure your user account has “Full Access” permission. In my scenario, I had to add the user account and I had to grant this permission:
9) After this adjust, I have tried to run the script again typing in the SharePoint Management Shell prompt: .\GetUsersWithoutProperty.ps1
The result was like this:
10) After that, I have just copied the “C:\UserProfiles.csv” generated by the script to my computer with Excel installed;
11) I have opened the Excel and I have navigated to the Tab “Data”. I have clicked on the “From Text” option:
12) I have selected the “UserProfiles.csv” file to import on Excel from the path I had saved it on my computer:
13) I have chosen the “Delimited” option and I have clicked on “Next” button:
14) I have chosen the “comma” option and I have clicked on “Next” button:
15) I have clicked on “Finish” button:
16) I have clicked on the “OK” button:
17) Now I can apply one filter on it to check following my needs. In that case, by “Pais” igual to “Empty“;
Ok, guys, that’s it. I hope it could be useful. If you have any doubt about it, please, feel comfortable to ask me.
No comments:
Post a Comment