Wednesday, October 14, 2015

Create mysite using Powershell

Function Create-MySite           
{           
 [CmdletBinding()]           
 Param           
 (           
  [Parameter(Mandatory = $True,Position=2,valueFromPipeline=$true)][String]$Username,           
     [Parameter(Mandatory = $True,Position=1)][String]$MySiteRootURL             
 )           
 [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");              
 $site=new-object Microsoft.SharePoint.SPSite($MySiteRootURL);           
 try           
 {           
  $serviceContext = Get-SPServiceContext $site;           
  $upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);            
  if($upm.UserExists($Username) -eq $false)           
  {           
   Write-Host "User $Username was not found in the profile store." -f yellow;           
   return;           
  }           
  $userProfile = $upm.GetUserProfile($Username);           
  if($userProfile.PersonalSite -eq $Null)           
  {           
   Write-Host "Creating MySite for user $Username" -f darkyellow;           
   $userProfile.CreatePersonalSite();                 
   Write-host "Successfully created MySite for user $Username" -f green;           
  }           
  else           
  {           
   Write-Host "User $Username already has a MySite." -f darkgreen;           
  }           
 }           
 catch           
 {           
  Write-Host "Encountered an error creating a MySite for user $Username. Error:"$_.Exception -f Red;           
 }           
 finally           
 {           
  $site.Dispose();           
 }           
}
Create-MySite -MySiteRootURL "http://url" -Username "domain\user"

Friday, October 9, 2015

Powershell script remove contenttypes from list


#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://URL
$lookForList = "ListName"
$ContentType = "CTName"
#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {

write-host "Checking site:"$_.Title

#Check list exists
$docLibrary = $_.Lists[$lookForList]

#Remove unwanted content types from the list
if($docLibrary -ne $null)
{
$ctToRemove = $docLibrary.ContentTypes[$ContentType]
write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
$docLibrary.ContentTypes.Delete($ctToRemove.Id)
$docLibrary.Update()
}
else
{
write-host "The list" $lookForList "does not exist in site" $_.Title
}
}
#Dispose of the site object
$site.Dispose()

Thursday, June 11, 2015

Export managed metadata

Get-SpServiceApplication | select DisplayName, ID
Get-SPServiceApplicationProxy | select DisplayName, ID
$mmsAppId = "GUID"
$mmsproxy = Get-SPServiceApplicationProxy -Identity GUID
Export-SPMetadataWebServicePartitionData -Identity $mmsAppId -ServiceProxy $mmsproxy -Path \\server\path\filename.bak

Migrate SharePoint social tag comments

Add-PSSnapin microsoft.sharepoint.powershell

$upaProxy = Get-SPServiceApplicationProxy GUID

$site = get-spsite #URL

$listName = #LISTNAME

foreach($web in $site.AllWebs){



$oldurl = ""

$newurl = ""

if($web.ParentWeb.Url -eq $site.url){



$list = $web.Lists[$listName]

$items = $list.items



foreach($item in $items)

{

if($item.xml -Like "*#RELPATH/PAGE.aspx, PAGETYPE*"){



$newurl = $web.url + "/" + $item.url

[System.uri]$newurl = $newurl

$newurl.AbsoluteUri



$oldurl = $newurl.AbsoluteUri

$oldurl = $oldurl -replace "SITE1", "SITE2"

$oldurl



Move-SPSocialComments -ProfileServiceApplicationProxy $upaProxy -OldUrl $oldurl -NewUrl $newurl.AbsoluteUri



}

}

}

}


Monday, July 7, 2014

Security Token Service is down or showing errors?



Some examples of errors that may show up in your ULS log.


Run the following commands, it will do a new provisioning of the Security Token Service.

$h = Get-SPServiceHostconfig
$h.Provision()
$services = Get-SPServiceApplication
foreach ($service in $services) { $service.provision();
write-host $service.name}


Perform an IIS Reset and try to browse the STS, http://localhost:32843/SecurityTokenServiceApplication/securitytoken.svc

Thursday, June 5, 2014

Set user default regional settings from website default

$spweb = Get-SPWeb http://yourURL.com
$user = get-spuser -Web $spweb -Identity "DOMAIN\USER"
$reg = $user.RegionalSettings
$newReg = new-object Microsoft.SharePoint.SPRegionalSettings($spweb);
$user.RegionalSettings = $newReg;
$user.Update();

Wednesday, June 4, 2014

Powershell script activate developer dashboard on demand in your Sharepoint 2013 environment

$svc = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$dds = $svc.DeveloperDashboardSettings
$dds.DisplayLevel = "OnDemand"
$dds.TraceEnabled = $true;
$dds.Update()

Monday, April 7, 2014

Add list column to list contenttype

$web = Get-SPWeb -Identity "URL to your site"
$list = $web.Lists["List name"]
$ct = $list.ContentTypes["Contenttype Name"]
$field = $list.Fields["Field Name"]
#$field = $list.Fields[{GUID}]
$fieldLink=New-Object Microsoft.SharePoint.SPFieldLink($field)
$ct.FieldLinks.Add($fieldLink);
$ct.Update()
$web.Dispose()

Wednesday, August 28, 2013

Count of all Documents in SharePoint 2010 Farm with Powershell


Get-SPSite -Limit All | Get-SPWeb -Limit All | % { $_.Lists} | ? { $_ -is [Microsoft.SharePoint.SPDocumentLibrary] } | % { $total+= $_.ItemCount} ; $total

Thursday, May 2, 2013

Open PDF

$webApp = Get-SPWebApplication http://url
If ($webApp.AllowedInlineDownloadedMimeTypes -notcontains "application/pdf")
{
  Write-Host "Adding PDF MIME Type..."
  $webApp.AllowedInlineDownloadedMimeTypes.Add("application/pdf")
  $webApp.Update()
  Write-Host "MIME Type saved."
} Else {
  Write-Host -ForegroundColor red "PDF MIME type is already added."
}