Tag Archives: powershell

PowerShell Script to delete items from SharePoint List

The criteria for the below script is to delete items that created before 7 days , you can put it as scheduale task to run weeklly to clear the large lists.

Add-PSSnapin Microsoft.SharePoint.PowerShell
[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$web = GetSPWeb “YOUR SHAREPOINT SITE”
$list = $web.Lists[“YOUR LIST NAME”]
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(7))
$caml=‘<Where> <Lt> <FieldRef Name=”Created” /><Value Type=”DateTime”>{0}</Value> </Lt> </Where> ‘ $DeleteBeforeDate
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml
$col=$list.GetItems($query)
WriteHost $col.Count
$col | % {$list.GetItemById($_.Id).Delete()}
$web.Dispose()

Courtesy: https://gallery.technet.microsoft.com/office/PowerShell-Script-to-f4e8ea90

Advertisements

Powershell all of subsites inherit from Parent site’s master page – SharePoint 2010

Execute the below script using powershell on the SharePoint server.

Get-SPWebApplication http://yourwebapp.com | Get-SPSite -Limit All | Get-SPWeb -Limit ALL | foreach-object{
    $_.MasterUrl = "/_catalogs/masterpage/masterurl.master"
    $_.CustomMasterUrl = "/_catalogs/masterpage/mypage.master"
    $_.Update()
}

Start and Stop SharePoint services using Powershell:

  1. Start and Stop SharePoint services using Powershell:

To Start:

$ServiceName = “Central Administration”

Get-SPServiceInstance -server spinrat2 | where-object {$_.TypeName -eq $ServiceName} | Start-SPServiceInstance -confirm:$false > $null

To Stop:

$ServiceName = “Visio Graphics Service”
Get-SPServiceInstance -server COMPUTERNAME | where-object {$_.TypeName -eq $ServiceName} | Stop-SPServiceInstance -confirm:$false > $null

Powershell, Identify the checkout files in sharepoint farm, SharePoint 2010

  1. Identify the checkout files in sharepoint farm:

function CheckedOutItems() {

write-host “Please enter the site url”

$url = read-host

write (“SiteURL`t” + “FileName`t” +  “CheckedOutTo`t” + “ModifiedDate`t”+”Version”)

$site = New-Object Microsoft.SharePoint.SPSite($url)

$webs = $site.AllWebs

foreach($web in $webs)

{

$listCollections = $web.Lists

foreach($list in $listCollections)

{

if ($list.BaseType.ToString() -eq “DocumentLibrary”)

{

$dList = [Microsoft.Sharepoint.SPDocumentLibrary]$list

$items = $dList.Items

$files = $dList.CheckedOutFiles

foreach($file in $files)

{

$wuse = $file.DirName.Substring($web.ServerRelativeUrl.Length)

Write ($web.Url+ “`t” + $wuse+”`/” + $file.LeafName +  “`t” + $file.CheckedOutBy.Name + “`t” + $file.TimeLastModified.ToString()+”`t” + “No Checked In Version” )

}

foreach($item in $items)

{

if ($item[“Checked Out To”] -ne $null)

{

$splitStrings = $item[“Checked Out To”].ToString().Split(‘#’)

Write ($web.Url+ “`t” + $item.Url + “`t” + $splitStrings[1].ToString() + “`t” + $item[“Modified”].ToString() +”`t” + $item[“Version”].ToString())

}

}

}

}

$web.Dispose()

}

$site.Dispose()

}

CheckedOutItems