Rename Central Admin Database


If you had ran Configuration Wizard to create your SharePoint Farm then your Admin Content database will have AdminContent_XXX-XXXX-XXXXX (Guid) in its name.  If you want to fix it, so you SQL Management studio looks good and no one actually blames you to be a bad SharePoint Admin, you can use the script below.

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#List all web applications
Get-SPWebApplication -IncludeCentralAdministration

$CentralAdminUrl = "http://sp2013:2013/"
$CentralAdminDB = Get-SPContentDatabase -WebApplication $CentralAdminUrl
#Verify the database object is good
$CentralAdminDB.Name

#New DB Name
$NewCentralAdminDBName = "01_Content_NewCentralAdmin"
$NewCentralAdminContentDB = New-SPContentDatabase -Name $NewCentralAdminDBName -WebApplication $CentralAdminUrl -MaxSiteCount 2 -WarningSiteCount 1
#Verify the database is added
Get-SPContentDatabase -WebApplication $CentralAdminUrl
#Verify the database object is good 
$NewCentralAdminContentDB
#Move the Sites to nicely named DB
$CentralAdminDB.Sites | % {Move-SPSite $_ -DestinationDatabase $NewCentralAdminContentDB -Confirm:$false -Verbose}
iisreset
#Verify the Site collection are moved
Get-SPContentDatabase -WebApplication $CentralAdminUrl
#If databases are moved then dismount the orignal DB

Dismount-SPContentDatabase $CentralAdminDB -Confirm:$false -Verbose
#Confirm First Database is gone
Get-SPContentDatabase -WebApplication $CentralAdminUrl
#Delete the database from SQL Server 

Update User Profile Feed Identifer after User Profile Re-Creation


The script can be used to re-create the user profile feed identifier property.  If this property is not set the user will not be able to use Social features like newsfeed on my site host.

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
$MySiteHost = new-object Microsoft.SharePoint.SPSite("https://mysites.contoso.com/");  
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($MySiteHost);  

$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext);  
$profile = $ProfileManager.GetUserProfile("contoso\adamb"); 
$Site = $profile.PersonalSite
$Site | Select *
$Site.RootWeb.Created
#If Personal Site needs to be created.  The feed Identifier will be added automatically in that case.
#$profile.CreatePersonalSite()
$profile.FeedIdentifier
#If My Site Already Exist.
$microFeedId = ($Site.Url + ";1." + $Site.ID.ToString().Replace("-", "") + "." + $Site.RootWeb.ID.ToString().Replace("-", "") + "." + $Site.RootWeb.Lists["Microfeed"].ID.ToString().Replace("-", "") + ".0c37852b34d0418e91c62ac25af4be5b")
$profile["SPS-FeedIdentifier"].Value = $microFeedId
$profile.Commit()
$Site.Dispose()
$MySiteHost.Dispose()

Usage Service Application Proxy Stopped on Central Admin


In case the proxy of usage service application is showed as stopped, the following PowerShell code can get it back to normal.

$Usage = Get-SPServiceApplicationProxy | where {$_.TypeName –eq “Usage and Health Data Collection Proxy”}
$Usage.Provision()

In my case it took couple of minutes to get back so be patient.  Basic but useful.

Powershell Script to Find Owners of Document Library SharePoint


Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

$SiteUrl = "https://portal.contoso.com"
$SiteCollection = Get-SPSite $SiteUrl
foreach($web in $SiteCollection.AllWebs)
{
    foreach($list in $web.Lists)
    {
        if($list.BaseType -eq "DocumentLibrary" -and $list.Hidden -eq $false)
        {
            if($list.Permissions.Inherited -eq $false)
            {
                foreach($Perm in $list.Permissions)
                {
                    if($Perm.PermissionMask -eq "FullMask")
                    {
                        Write-Host $web.Url, $list.Title $Perm.Member -ForegroundColor Green
                    }
                }
            }
        }
    }
    $web.Dispose()
}
$SiteCollection.Dispose()

PowerShell to Force Check In Document in a Libarary


Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# define Variables
$SiteUrl = “http://portal.contoso.com/subsite”
$Library = “Documents”
$fileName = “FileName.docx”
$web = Get-SPWeb $SiteUrl
$Library = $web.GetFolder($Library)
$File = $Library.Files | ? {$_.name -eq $fileName}

$File.CheckIn(“Undo Checkout by force”)

$File.UndoCheckOut()
$File.Update()

Add and Remove Web Site Binding using PowerShell


Windows Server 2008 R2 does not allow us to add a binding to same port twice.  Additionally you can not select a different SSL certificate for web app that is configured with https.  To do so you have to add binding by adding another IP address on the server and then add the binding on the IP and use a different SSL cert.  I saw people using a comand line utility like appcmd but frankly the syntax was pretty rough to use it.  So I tried wth PowerShell.  I was able to make a script that allows you to add a remove a binding as required.  The below script is used when I configured SharePoint Hosted apps on Windows server 2008 R2 environment where apps are configured to use different app domain.  Customer was getting certificate error so they wanted to use different SSL cert on App Web (No Host Header Web Application).  Just like I said before you just need an addition IP address on the WFES and then add a binding with * on that IP.  You can then remove the default binding using the same PowerShell script.

Import-Module WebAdministration
Get-Website
#Note the Web application Name
$ADFSApp = Get-Website -Name "NoHostHeader"
New-WebBinding -Name "NoHostHeader" -Protocol https -Port 443 -HostHeader "*" -IPAddress 35.0.0.11
#List the binding of the web appGet-WebBinding -Name "NoHostHeader" | Select *
#Remove the default Binding
Remove-WebBinding -Name "NoHostHeader" -BindingInformation "*:443:"

You can change the commands the way you want.  Do not try to modify the values of  https binding from UI as the changes apply to all web applications.  Be careful.  The script only targets one web app it is safe to execute.

Update Hidden View with PowerShell


Here is quick fix for an issue where AllItems view was set to blank and was set to hidden.  Here is how I fixed it.  Nothing fancy just some old Server OM calls in PowerShell.

First you have to list all the views to find it out what’s going wrong and put it in a list.

$SiteUrl = https://portal.contoso.com     
$ListUrl = "Discussions List"            

$Databases = @();
       
$Web = Get-SPWeb -Identity $SiteUrl            
if ($Web -ne $null)            
{
    $List = $Web.Lists[$ListUrl]
    foreach($view in $List.Views)
    {
        $ViewFile = $targetUrl.GetFile($view.Url)
        $Viewobj = New-Object PSObject
        Add-Member -input $Viewobj noteproperty 'ID'  $view.ID
        Add-Member -input $Viewobj noteproperty 'Title'  $view.Title
        Add-Member -input $Viewobj noteproperty 'CreatedBy'  $ViewFile.Author
        Add-Member -input $Viewobj noteproperty 'Modified' $ViewFile.TimeLastModified 
        Add-Member -input $Viewobj noteproperty 'ModifiedBy' $ViewFile.ModifiedBy 
        Add-Member -input $Viewobj noteproperty 'CreatedOn' $ViewFile.TimeCreated  
        Add-Member -input $Viewobj noteproperty 'Private' $view.PersonalView  
        Add-Member -input $Viewobj noteproperty 'Url' $view.Url  
        Add-Member -input $Viewobj noteproperty 'Hidden' $view.Hidden  
        $Databases += $Viewobj
          
    }
    #Export If Needed.        
    #$Databases  | Out-File C:\View.txt

}
#Select the View and Click OK in GridView
$aBadView= $Databases | Out-GridView -PassThru
$aBadView.ID

Now You have the View ID you have do it like below

$ID = [guid]("0b57d149-dbcc-4652-b72f-2fa024238301")    #$ID = [guid]($aBadView.ID)
$BadView = $List.Views[$ID]
$BadView.Title = "Subject"
$BadView.Hidden = $false
$BadView.Update()

Run the first part of script again to verify.