This script can be used to automate the user profile synchronization for the default User Profile Service Application.
It can be used to schedule the user profile sync operation or start it after a farm restore operation.
The script uses the default user profile service application to import the profiles from. In a production farm, there would be only one User Profile Service application and this script will import profiles for that. The synchronization is always incremental and it will not run if the sync is happening now.
#This script is used start user profile synchronization in SharePoint 2010. Written by Harikumar
#Load SharePoint assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Powershell")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Admin")
function Get-SPServiceContext([Microsoft.SharePoint.Administration.SPServiceApplication]$profileApp)
{
if($profileApp -eq $null)
{
#----- Get the first User Profile Service Application
$profileApp = @(Get-SPServiceApplication | ? { $_.TypeName -eq "User Profile Service Application" })[0]
}
return ([Microsoft.SharePoint.SPServiceContext]::GetContext(
$profileApp.ServiceApplicationProxyGroup,
[Microsoft.SharePoint.SPSiteSubscriptionIdentifier]::Default))
}
$serviceContext= Get-SPServiceContext
$configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)
if($configManager.IsSynchronizationRunning() -eq $false)
{
$configManager.StartSynchronization($true)
Write-Host "Started Synchronizing"
}
else
{
Write-Host "Already Synchronizing"
}
#This script is used start user profile synchronization in SharePoint 2010. Written by Harikumar#Load SharePoint assemblies [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Powershell") [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles") [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Admin") function Get-SPServiceContext([Microsoft.SharePoint.Administration.SPServiceApplication]$profileApp) { if($profileApp-eq $null) { #----- Get the first User Profile Service Application$profileApp = @(Get-SPServiceApplication | ? { $_.TypeName -eq "User Profile Service Application" })[0] } return ([Microsoft.SharePoint.SPServiceContext]::GetContext( $profileApp.ServiceApplicationProxyGroup, [Microsoft.SharePoint.SPSiteSubscriptionIdentifier]::Default)) } $serviceContext= Get-SPServiceContext $configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext) if($configManager.IsSynchronizationRunning() -eq $false) { $configManager.StartSynchronization($true) Write-Host "Started Synchronizing" } else { Write-Host "Already Synchronizing" }
Copy the script to a folder and then run it from there. The following screenshot shows the two running scenarios.

If you run the script again, then it will tell you that it is already synchronizing.

The following line in the script fetches the User Profile application object.
$profileApp = @(Get-SPServiceApplication | ? { $_.TypeName -eq "User Profile Service Application" })[0]
If you have more than one User Profile service application configured, then you can get to a different UPA by changing the value [0] to [1] or [2].
This script will only work in SP 2010 and with the first User Profile service application.