Create My Site for All Users in SharePoint using PowerShell

SharePoint My Sites are automatically created when users click on the My Content link in the welcome menu, for the first time. However, To promote the usage of SharePoint social, we deiced to pre-create SharePoint My sites for all users.

create mysite for all users in sharepoint using powershell

Create My site for All Users in SharePoint using PowerShell

Here is how to create a “My site” for a SharePoint user (If it’s not created already!):

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
Function Create-MySite($MySiteHost, $UserAccount) 
{
    #Get Objects
    $ServiceContext  = Get-SPServiceContext -site $MySiteHost
    $UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

    #Check if user Exists
    if ($UserProfileManager.UserExists($UserAccount)) 
    {
        #Get the User Profile
        $UserProfile = $UserProfileManager.GetUserProfile($UserAccount)
        
        #Check if User's My site is Cretaed already
        if($UserProfile.PersonalSite -eq $Null)
        {
            $UserProfile.CreatePersonalSite()
            write-host "My Site Created Successfully for the User!" -f Green
        }        
    }
    else
    {
        write-host "$($UserAccount) Not Found!" -f Red
    }
}

Create-MySite "https://mysite.crescent.com" "Crescent\Salaudeen"

SharePoint: Create My site for all users using PowerShell:

As a SharePoint administrator, you want to make sure My sites are provisioned for all users in the organization. In this guide, we’ll show you how to use PowerShell to pre-create My sites for everyone.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Variables
$MySiteHost = "https://mysite.crescent.com"
 
#Get Objects
$ServiceContext  = Get-SPServiceContext -site $MySiteHost
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get All User Profiles
$UserProfiles = $UserProfileManager.GetEnumerator()

#Iterate through each profile
foreach ($Profile in $UserProfiles)
{
    if($Profile.PersonalSite -eq $Null)
    {
        #Check if User's My site is Cretaed already
        if($Profile.PersonalSite -eq $Null)
        {
            #$UserProfile.CreatePersonalSite()
            write-host "My Site Created Successfully for the User:" $Profile["AccountName"] -f Green
        }
        else
        {
            write-host "My Site Already Exists for the User:" $Profile["AccountName"] -f Red
        } 
    }
}

BTW, I’ve tested this script in SharePoint 2013 to create my site for all users!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *