Navigate Up
Sign In

 
 
Creating MySites for all users in the Profile Database
While working on migrating content from one sharepoint farm to another, I needed a way to provision user mysites so that I could import the content that I exported using the export-spweb command for each person. After piecing together a script that reads the profile database and creates a site (if one does not already exist) for each user. I wanted to share this in case it comes in handy for anyone else.
Here is the script. Make sure to change the $siteurl= line to reflect your mysite url.
 
 
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")    
$siteurl = "http://MY_SITE_WEB_NAME"
     
$site = New-Object Microsoft.SharePoint.SPSite($siteurl) 
$context = [Microsoft.Office.Server.ServerContext]::GetContext($site) 
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)   
$upm.Count;
$profiles = $upm.GetEnumerator()
foreach ($userobject in $Profiles ) {      
 # start loop here     
 $user = $userobject.item("AccountName")    
 $user    
 if ($upm.UserExists($user)) {         
  $profile = $upm.GetUserProfile([string]$user)                
  
  # there are other exceptions you can catch, check out the UserProfiles class         
  trap [Microsoft.Office.Server.UserProfiles.PersonalSiteExistsException] {             
   Write-Host "personal site already exists for $user"            
   continue         
  }          
  $profile.CreatePersonalSite();        
  trap {        
  Write-Host "Error creating site for $user" -ForeGroundColor Red        
  continue        
  }        
  Write-Host "Personal site for $user created..." -ForeGroundColor Green    
 }  else {       
 Write-Host: "user $user did not exist"    
 } 
}
# end loop here 
$site.Dispose()
© 2012 EMS Solutions Group. All rights reserved