Skip to content

Instantly share code, notes, and snippets.

@ciphertxt
Created March 26, 2013 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciphertxt/5244828 to your computer and use it in GitHub Desktop.
Save ciphertxt/5244828 to your computer and use it in GitHub Desktop.
Adds all users in the UPA to a given site collection. Handy for pre-populating a User Information List
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# Enter your SharePoint site URL here...
$site = new-object Microsoft.SharePoint.SPSite("http://intranet");
$web = Get-SPWeb "http://intranet";
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()
$allProfileCounter = 0;
$usrProfileCounter = 0;
foreach($profile in $AllProfiles)
{
$allProfileCounter = $allProfileCounter + 1;
$DisplayName = $profile.DisplayName
$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
$shortAccountName = $AccountName.Substring($AccountName.IndexOf("\") + 1);
$web.EnsureUser($AccountName);
write-host "Added $DisplayName ($shortAccountName) to site";
$usrProfileCounter = $usrProfileCounter + 1;
}
write-host "Found $usrProfileCounter users out of $allProfileCounter profiles";
write-host "Finished."
$site.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment