Monday, August 17, 2009

Playing around with Powershell and SharePoint user profiles

First thing to note when playing around with SharePoint and Powershell is that you will probably need to load a number of extra assemblies (For some background check out Load an assembly in Powershell).

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("microsoft.sharepoint.portal")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[System.Reflection.Assembly]::LoadWithPartialName("System.Web")


Next if you want to manipulate SharePoint user profiles using Powershell you might want to take a look at Creating and updating  user profile properties in SharePoint using Powershell



Now an interesting concept would be to use a XML file as a source for creating SharePoint user profiles – take a look at Processing XML with Powershell to get going. So basically if you want to build a reusable powershell script for creating user profiles you will need two parameters – the XML file and the name of the SSP. By adding in some extra functions and using the power of piping (See Piping and Pipeline in Powershell) you will have a very powerfull powershell script with only limited code.




$userprofileproperties = get-xml $xmlFile

$context = [Microsoft.Office.Server.ServerContext]::GetContext($sspName)
$profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

$userprofileproperties.userprofileproperties.userprofileproperty | foreach { checkIfExists $_.Name $profileManager }
$userprofileproperties.userprofileproperties.userprofileproperty | foreach { createProperty $_ $profileManager $context }



PS I’m not going to give you all the code but the code snippets above should get you going. Credits go to Jeroen for writing these nifty lines of powershell.




1 comment:

Jeremy Thake said...

The SPDevWiki has more detailed PowerShell scripts around User Profiles (and others)