Powershell Scripts


PowerShell Tools  : 

Link : http://www.quest.com/powergui-freeware/

Output will be  : 






To get SharePoint Farm GUID:

 $SpFarm=get-SpFarm

 $SpFarm.ID


Creating web application in Windows authentication using PowerShell

New-SPWebApplication -Name Sp_Demo_2013 -ApplicationPool App_Sp_Demo_2013 -ApplicationPoolAccount inside\gannara1 -Port 9595 -Url  http://winapwebdv1 -DatabaseName WSS_Sp_demo_content_9595 -Verbose


Database Test

Test-SPContentDatabase -name WSS_Content_DB -webapplication http://sitename


Mounting Database :

Mount-SPContentDatabase -Name Wss_Dev_TeamRooms -DatabaseServer winapwebdv1 -WebApplication http://winapwebdv1:9595


Dismount Database:

Dismount-SPContentDatabase "<ContentdBName>" -Confirm:$false


Getting Feature Name from ID

Get-SPFeature –Limit ALL | Where-Object {$_.Scope –eq "SITE"}

Activating Feature with ID :

Get-SPWebApplication http://localhost:9292 | Get-SPSite -Limit ALL | % {Enable-SPFeature "716f0ee9-e2b0-41f0-a73c-47ed73f135de" -Url $_.Url}




SharePoint Farm Password reset :

stsadm.exe -o updatefarmcredentials -userlogin  domain\Amar -password xxxssfdss



Site Collection Back-up


Backup-SPSite -Identity http://abc/corporate/it -Path "C:\backups\it.bak" -Force -Verbose


Site Collection Restore

Restore-SPSite -Identity <SiteCollectionURL> -Path <Backup file>


Feature Activation

Enable-SPFeature –identity "MyCustom" -URL http://somesite

Site collection size:


Get-SPSite | select url, @{label="Size in MB";Expression={$_.usage.storage}}

In bits : 

Get-SPSite -Limit All | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | Format-Table –AutoSize

Get-SPSite | select url, @{label"Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | ConvertTo-Html -title "Site Collections sort by size" | Set-Content sc.html
 

 

 

Disable the self-service evaluation

 


$siteUrl = "http://localhost";
$site = Get-SPSite $siteUrl;
$site.AllowSelfServiceUpgrade = $false;
$site.Update();
 
 
 Getting Site Collection Admins list in SharePoint Site : 

Add-PSSnapin Microsoft.SharePoint.Powershell

$root = New-Object Microsoft.SharePoint.SPSite("http://localhost:4343")
$spWebApp = $root.WebApplication

foreach($site in $spWebApp.Sites)
{
      $name = $site.RootWeb.Title
      $admins = $site.RootWeb.SiteAdministrators
      $url=$site.RootWeb.Url
      $array = @($name, $admins,$url) |Format-Table -Wrap -AutoSize
      $array | Out-File C:\Db\admins.txt -Append
      $site.Dispose()

}
$root.Dispose()
 

Enabling Developer Dash boarding : 

$ErrorActionPreference = 'Stop'
# Load SharePoint PowerShell snapin
Write-Host
Write-Host " Step 1 of 2: Loading SharePoint PowerShell Module" -ForegroundColor Black -BackgroundColor Gray
Write-Host
Write-Host " Verify SharePoint PowerShell Snapin Loaded" -ForegroundColor White
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.PowerShell'}
if ($snapin -eq $null) {
Write-Host " .. loading SharePoint PowerShell Snapin..." -ForegroundColor Gray
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Write-Host " Microsoft SharePoint PowerShell snapin loaded" -ForegroundColor Gray
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$devDashboardSettings = $contentService.DeveloperDashboardSettings
Write-Host
Write-Host " Turning on Developer Dashboard" -ForegroundColor White
if ($devDashboardSettings.DisplayLevel -eq "On"){
$devDashboardSettings.DisplayLevel = "Off"
Write-Host "Developer dashboard disabled." -ForegroundColor Gray
} else {
$devDashboardSettings.DisplayLevel = "On"
Write-Host "Developer dashboard enabled." -ForegroundColor Gray
}
$devDashboardSettings.Update()
Write-Host
Write-Host "Finished!" -ForegroundColor Green

 
 
 
 
 
 
 
 WSP Deployment : 
 
 
 
 Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue 

$SolutionPackageName = "ABCProject1.wsp" 
$SolutionPackagePath = "ABCDevProject1_v2000\ABCProject1.wsp" 

$solution = Get-SPSolution | where-object {$_.Name -eq $SolutionPackageName} 

if ($solution -ne $null)
 {
  
   if($solution.Deployed -eq $true)
   {  
   Uninstall-SPSolution -Identity $SolutionPackageName -Local -Confirm:$false
   }

   Remove-SPSolution -Identity $SolutionPackageName -Confirm:$false 
} 

 Write-Host "Installing Solution..." 

 Add-SPSolution -LiteralPath $SolutionPackagePath 

 Install-SPSolution -Identity $SolutionPackageName -Local -GACDeployment

 Write-Host "Deployment Complete"
 
 
  

 Updating a Farm Solution

 
 $SolutionPackageName = "ABCProject1.wsp"
 $SolutionPackagePath = "ABCDevProject1_v2000\ABCProject1.wsp" 
 
 Update-SPSolution -Identity $SolutionPackageName -LiteralPath
 $SolutionPackagePath -Local -GACDeployment
 
 
 
 Configure Farm Admin Group : 

 Add-PSSnapin microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue 

      Get-spwebapplication –includecentraladministration 
      $web = get-spweb http://370lw7e08617:43723/
      $username="testdomain\sai"
      $group = $web.sitegroups[“Farm Administrators”]
      $group.adduser($username,””,$username,””)
      $group.update

 
 Setting Outgoing Email in Sharepoint 2010\2013 Farm: 

 

$CadminWebApplicatioin = get-spwebapplication http://370lw7e08617:43723
$SMTPSvr = 'mail.smtp.com'
$FromAddr = 'noreply@testdomain.com'
$ReplyAddr = 'noreply@testdomain.com'
$Charset = 65001
$CadminWebApplicatioin.UpdateMailSettings($SMTPSvr, $FromAddr, $ReplyAddr, $Charset)



 For more information : http://mikefrobbins.com/2011/08/04/configure-sharepoint-2010-outgoing-email-settings-with-powershell/


 
 
Site Usage Report : 
 
System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$Siteurl="http://localhost:9292";

$Output="C:\SiteUsage.csv";

"Site URL"+","+"Owner Login"+","+"Owner Email"+","+"Root Site Last Modified"+","+"Quota Limit (MB)"+","+"Total Storage Used (MB)"+","+"Site Quota Percentage Used" | Out-File -Encoding Default -FilePath $Output;
#Specify the root site collection within the Web app$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;
#Loops through each site collection within the Web app, if the owner has an e-mail address this is written to the output file
Foreach ($Site in $Webapp.Sites)
{if ($Site.Quota.Storagemaximumlevel -gt 0) {[int]$MaxStorage=$Site.Quota.StorageMaximumLevel /1MB} else {$MaxStorage="0"}; 
if ($Site.Usage.Storage -gt 0) {[int]$StorageUsed=$Site.Usage.Storage /1MB};
if ($Storageused-gt 0 -and $Maxstorage-gt 0){[int]$SiteQuotaUsed=$Storageused/$Maxstorage* 100} else {$SiteQuotaUsed="0"}; 
$Web=$Site.Rootweb; $Site.Url + "," + $Site.Owner.Name + "," + $Site.Owner.Email + "," +$Web.LastItemModifiedDate.ToShortDateString() + "," +$MaxStorage+","+$StorageUsed + "," + $SiteQuotaUsed | Out-File -Encoding Default -Append -FilePath $Output;$Site.Dispose()};
 

Getting Database Size in the SharePoint Farm :

 
 
Get-SPDatabase | ForEach-Object {$db=0}
 {$db +=$_.disksizerequired; $_.name + " - " + $_.disksizerequired/1024/1024}
 {Write-Host "`nTotal Storage (in MB) =" ("{0:n0}" -f ($db/1024/1024))} 
 
 
 
Creating New site collection in Different Database : 
Step1 :
New-SPContentDatabase -Name WSS_Content_HR -WebApplication  http://vssharepoint2010
Step2 :
New-SPSite http://vssharepoint2010/sites/hr -OwnerAlias “abc\Administrator” -ContentDatabase WSS_Content_HR -Name “HR” -Description “HR Solutions” -Template “STS#0″



$web = get-spweb http://sharepoint
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “<Custom Templete Title>”}
New-SPWeb -Url “https://sharepoint/newsubsite” -Template “{D839E38A-F9DF-4B35-BDA7-A1E273C51EAB}#XXXX Template”
 
 
Refer : Site templates : 
 
 
Finding site template names and ID’s in SharePoint using PowerShell 
 Get-SPWebTemplate | Sort-Object "Name"

$web = Get-SPWeb http://DemoSite
write-host "Web Template:" $web.WebTemplate " | Web Template ID:" $web.WebTemplateId
$web.Dispose()

$template = Get-SPWebTemplate | where { $_.Title -eq "Team Site" }
 SharePoint Farm Passphrase


$passphrase = ConvertTo-SecureString -asPlainText –Force
Set-SPPassPhrase -PassPhrase $passphrase –Confirm
 
 
 
 PowerShell Script for List of Servers restart : 

  
 
 #Specify servers in an array variable
[array]$servers = "Server1","Server1","Server1","Server1"
#Step through each server in the array and perform an IISRESET
#Also show IIS service status after the reset has completed
foreach ($server in $servers)
{
    Write-Host "Restarting IIS on server $server..."
    IISRESET $server /noforce
    Write-Host "IIS status for server $server" -foregroundcolor "Red"

    IISRESET $server /status
}
Write-Host IIS has been restarted on all servers -foregroundcolor "magenta"

 
 
 Sharepoint ULS lOgs : 
 
 
 Merge-SPLogFile -Path ".\error.log" -Correlation "5ca5269c-8de5-4091-3f1b-f179af4d5121"
 
 

 

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite
This example merges the last hour of log data from all farm computers with no filtering.

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite -Area Search
This example merges the last hour of log data from the Search area.
 

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite -Area "SharePoint Foundation","Web Analytics Services"
 
 
 
his example merges the last hour of log data from the SharePoint Foundation and Web Analytics Services areas.
 
 
 
Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite -Level High

This example merges the log data of level High or higher.


Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite -StartTime "06/09/2008 16:00" - EndTime "06/09/2008 16:15"
 
This example merges the log data for events in a particular time range, which is culture-specific to the United States.

Merge-SPLogFile -Path "C:\Logs\FarmMergedLog.log" -Overwrite -Message "*permission changed*"
 
 This example merges the log data for events with permission changed in the message text.

Merge-SPLogFile -Overwrite -Path d:\1.log -ContextFilter "name=timer job*" -Area "*search*"
 
 
 
 This example merges the log data for all search timer jobs.
 
 
 
 Merge-SPLogFile -Overwrite -Path d:\2.log -ContextFilter "user=contoso?joeuser"


 This example shows how to merge the log data for all user names that have a contoso\joeuser or Contoso/joeuser format.
 
 
 
 Inventory SharePoint Farm Content PowerShell Script

 
 https://gallery.technet.microsoft.com/office/Inventory-SharePoint-Farm-dc11fc28/view/Discussions

 
   Other Links :

 
 
 
Script to Get List Column Title and Internal Names . 

$web = Get-SPWeb  http://Localhost:4343
$list = $web.Lists["ListName"]
$list.fields | select Title, InternalName, Hidden,type| sort title |Out-File c:\CIP\Sp2013POC.txt -width 220

$list.fields | select Title, InternalName, Hidden| where {$_.Hidden -eq $false}
Export-Csv -path c:\CIP\test.csv -NoTypeInformation
 
 
 
 
PowerShell script output to file : 


$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# do some stuffStop-Transcript
 
 
 

 Viewing Truncated PowerShell Output
 
 
 | Format-Table -Property * -AutoSize `
 | Out-String -Width 4096 `
 | Out-File C:\aliases.txt
 

Before :



 

After :

 
 
 
 
 
 

 Sharepoint Farm Script :


https://sharepointpsscripts.codeplex.com/releases/view/112556
 
 
MSDN Scripts :
 
 
 
SharePoint Timer changing .
 
$SPSite = Get-SPSite("http://localhost")
$webApp = $SPSite.WebApplication
 $webApp.FormDigestSettings    

$webApp.FormDigestSettings.Timeout = New-TimeSpan -Hours 8 
 $webApp.Update()
 
 
 
SharePoint Powershell Scripts :
 
 
 
 
 
  • Web application cmdlets
  • Site management cmdlets

  • Deleting web Application :
     

    Get-SPWebApplication http://webapp.portal.com/ | Remove-SPWebApplication -Zone "Internet" -Confirm
    Remove-SPWebApplication http://webapp.portal.com/  -Confirm -DeleteIISSite -RemoveContentDatabases



     
     
     
    Service Application Creation : 
     
     
    http://social.technet.microsoft.com/wiki/contents/articles/12512.sharepoint-2013-service-applications-list.aspx
     
     
     
     
     
     
     
     
     Apps configuration : 
     
     

    1 comment:

    1. For direct imperativeness fans you can search for after music, movies, accounts and really on the web. Despite whether its news blogs concentrated on the dispatch of a film or covering the activities of your favored star, there's information open as it happens. https://mediosindependientes.org/

      ReplyDelete