From time to time I need to set up a somewhat realistic testing environment, especially recently when I’ve been playing around with personal sites and setting up profiled dashboards for different users ( Tech support, IS Manager, Line of Business) in order to test out applying certain pages or webparts to specific roles (Which I don’t really want to run against production just quite yet).  I don’t know how useful this would be in a production environment, however it comes in very handy when I’m setting up test environments and want to fill out the MySite area with a bunch of personal site collections for testing.  I could just set up a replication from production or copy the profile database from production, but this way I don’t even need to touch production to get what I need for testing purposes.

The key here is just to run through the User Profile Manager and get all the user profiles in the UPM and then if the Profile’s .PersonalSite property is $Null we go ahead and create the site for them.  At the end, don’t forget to .Dispose the SPSite!

I also go through and set up an SP personal admin account to be admin for all personal sites we create.  This is so that later when it’s in production, I can give someone in HR the rights to review everyone’s personal sites without having to give them Farm Admin rights, because let’s face it that’s just a scary idea.

# Code to create personal site for users if they don't already have one.
# Assign a Personal Site Admin account to each PersonalSite so we don’t need to give Farm Admin

#Add our SnapIns...
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

#Set up script variables
$mysiteHostUrl = "http://MySite"
$personalSiteGlobalAdminSAM = "DOMAIN\sp_p_admin"
$personalSiteGlobalAdminEmail ="sp_p_admin@iedaddy.com"
$personalSiteGlobalAdminDisplayName = "SharePoint Personal Sites Admin"

#Now load up our objects
$mysite = Get-SPSite $mysiteHostUrl
$context = [Microsoft.Office.Server.ServerContext]::GetContext($mysite)
$upm =  New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$AllProfiles = $upm.GetEnumerator()

#Loop through all our profiles
foreach($profile in $AllProfiles)
{
    #Get display namee and SAM account
    $DisplayName = $profile.DisplayName
    $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
       #Make sure they are in our DOMAIN
        if($Accountname -like "DOMAIN*")
       {
         #Check if personal site already exists
          if($profile.PersonalSite -eq $Null)
          {
               #Lets go ahead and create the personal account
               write-host "Creating personal site for ", $AccountName
               $profile.CreatePersonalSite()
          }
          else
          {
               write-host "Personal site already exists for ", $AccountName
          }       

       #Good to add an SPadmin so it makes it easier to check the personal site stuff for role based profiles w/o Farm Admin rights        
       $pweb = $profile.PersonalSite.OpenWeb()
       #Put the padmin into AllUsers list and make them an admin
       $pweb.AllUsers.Add($personalSiteGlobalAdminSAM,$personalSiteGlobalAdminEmail,$personalSiteGlobalAdminDisplayName,$null);
       $padm= $pweb.AllUsers[$personalSiteGlobalAdminSAM];
       $padm.IsSiteAdmin = $true;
       $padm.Update();
       $pweb.Dispose(); 
       write-host "Personal Site Admin has assigned"
   }
}

#All done, remember to dispose of SPSite object!
$mysite.Dispose();