Sharepoint Powershell Quick Reference

#################################################
############### Jayaraja ####################
############### http://34.231.97.77 ####################
############## Bugs or Thoughts are welcome 🙂 sweetraskels@gmail.com ####
################################################

#Add Sharepoint Powershell Snap if its not loaded
$powershellSnapin = “Microsoft.Sharepoint.Powershell”
if ((Get-PSSnapin -Name $powershellSnapin -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host “——————————— Adding sharpoint powershell snapin ————————-”
Add-PsSnapin $powershellSnapin
}

 

if(($args[0] -eq ‘Tech’) -or ($args[0] -eq $null) -or ($args[0] -eq “”)) {
$site = new-object Microsoft.SharePoint.SPSite(“url of ur iso environment”)

}
elseif($args[0] -eq ‘Dev’) {
$site = new-object Microsoft.SharePoint.SPSite(“dev environment url”)
}
elseif ($args[0] -eq ‘Model’) {
$site = new-object Microsoft.SharePoint.SPSite(“Model environment url”)
}
elseif ($args[0] -eq ‘Prod’) {
$site = new-object Microsoft.SharePoint.SPSite(“Production environment url”)
}
else {
Write-Host “$(Get-Date -Format o) Environment not specified – please pass appropriate environment to deploy -”
exit
}

 

#function to check for the timer job status
function CheckTimerJobStatus ( [String]$TimerJobName)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
$isDisabled=Get-SPTimerJob $TimerJobName | select IsDisabled
return $isDisabled
}

#function to enable the timer job if its disabled
function EnableTimerJobIfDisabled ( [String]$TimerJobName)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
if(CheckTimerJobStatus)
{
Get-SPTimerJob $TimerJobName
Enable-SPTimerjob -Identity $TimerJobName
write-host “Enabled $TimerJobName”
}
}

#Disable the Timer Job if its Enabled
function DisableTimerJobIfEnabled ( [String]$TimerJobName)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
Get-SPTimerJob $TimerJobName | Disable-SPTimerJob
write-host “Disabled $TimerJobName”
}

#use this to check the timer job status
#CheckTimerJobStatus -TimerJobName “nameofthetimerjob”
function Upload-SPFile ( [String]$file, [string]$list,[string]$sweb)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

if($sweb -ne “Root”)
{
$web = $site.AllWebs | where { $_.name -eq $sweb}
}
else
{
$web = $site.RootWeb
}
$spList = $web.GetFolder($list)
$spFiles = $spList.Files
$docLibrary = $web.Lists[$list]
$folder = $docLibrary.RootFolder
$docName =Split-Path $file -Leaf
$spFilePath = (“/” + $DocName)
$spFullPath = $folder.Url + $spFilePath
$fileInfo = Get-ChildItem $file
$fileContent = Get-Content $fileInfo -encoding byte
$fileName = “{0}/{1}” -f $list, $fileInfo.Name
$fileMetadata = New-Object HashTable
write-host “Uploading….”
write-host “List:” $list
write-host “Document:” $docName
$spFiles.Add($fileName, $fileContent, $fileMetadata, $true)
write-host “Verifying…”
write-host “List:” $list
write-host “Document:” $docName
write-host “Status: ” $web.GetFile($spFullPath).Exists
}

 

function Delete-List ([string]$sweb,[string]$list)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Sharepoint”)

if($sweb -ne “Root”)
{
$web = $site.AllWebs | where { $_.name -eq $sweb}
$splist=$web.Lists.TryGetList($list)
if($splist -ne $null)
{
write-host “Deleting …..”
$web.Lists.Delete([System.Guid]$web.Lists[$list].ID)
}
else
{
write-host $list “List Does Not Exist in” $web
}
}
else
{
$web = $site.RootWeb
$splist=$web.Lists.TryGetList($list)
if($splist -ne $null)
{
write-host “Deleting …..”
$web.Lists.Delete([System.Guid]$web.Lists[$list].ID)
}
else
{
write-host $list “List Does Not Exist in” $web
}
}
}
#Uploads the list template to the site template gallery
function UploadListTemplate ( [String]$fileName)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
write-host “Uploading $File Template to List Template Gallery”
$web = $site.RootWeb
$spFolder = $web.getfolder(“List Template Gallery”)
$spfilecollection = $spfolder.files
$file = get-item $fileName
$spfilecollection.Add(“_catalogs/lt/$fileName”, $file.OpenRead(), $true)
}
#Create List from templates (list template gallery)
function CreateCustomListFromTemplate([string]$listName,[string]$templateName)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
write-host “Creating Custom List($listName) from List Template $templateName”
$web = $site.RootWeb
$templates=$site.GetCustomListTemplates($web)
$exist=0
foreach($template in $templates)
{
write-host Verifying $templateName in “List Template Gallery”
#$template.Name
if($template.Name -eq $templateName)
{
Write-host ” Template Found”
$listTemplates = $site.GetCustomListTemplates($web)
Write-host “Creating…..”
$web.Lists.Add($listName, $listName, $listTemplates[$templateName])
Write-host “Created List ” $template.Name
exit
}
else
{
write-host “Cant verify the existance of the template in the Template Gallery”
}
}
}
#Mofies the column name from x to y, eg” renaming title to any string say xyz in the list
function ModifyColumnName([string]$sweb,[string]$ListName,[string]$field,[string]$value)
{
if($sweb -ne “Root”)
{
$web = $site.openweb($web)
}
else
{
$web = $site.RootWeb
}
$spList = $web.Lists[$ListName]
$spAssignment = Start-SPAssignment
$spList.Fields[$field]=$value
write-host $column
$spList.Update()
Stop-SPAssignment $spAssignment

}
function AddItemLinkList([string]$sweb,[string]$slist,[string]$desc,[sting]$url,[string]$notes)
{
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
Write-host “Adding items to the Link List”
if($sweb -ne “Root”)
{
$spweb = $site.openweb($web)
}
else
{
$spweb = $site.RootWeb
}
$list = $spweb.Lists[$slist]
$item = $list.Items.Add()
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue(“”)
$urlValue.Description = $desc;
$urlValue.Url = $itemUrl
$item[“URL”] = $url
$item[“Notes”]=$notes
$item.update()
$List.update()
$spweb.Dispose()
$site.Dispose()
}

 

 

################### EXECUTION ########################
##### Create List From Template #######
###this will write the exception to the log and keep running
trap
{
Write-Host
Write-Host $_.exception.message
continue
}

###create list from the template
#CreateCustomListFromTemplate -listName “listname” -templateName “listtemplatename”
#### Upload List Template ####
#UploadListTemplate -file “listtemplate.stp”

 

#### Delete List ####
#use sweb parameter as the web name if not use Root for root site
#Delete-List -sweb “subsitetitle” -list “testing”
#Delete-List -sweb “Root” -list “test”

#### Upload File to Document Library ####
#use sweb parameter as the web name if not use Root for root site
#Upload-SPFile -list “testing” -file “./filename.extension” -sweb “subsite”
#Upload-SPFile -list “test” -file “./filename.extension” -sweb “Root”

#### Check Timer Job Status ####
#use this to check the timer job status
#CheckTimerJobStatus -TimerJobName “timerjobname”

#### Enable Timer Job ####
#Enable the timer job if it is disabled
#EnableTimerJobIfDisabled -TimerJobName “timerjobname”

#### Disable Timer Job ####
#Disbale the timer job if it is enabled
#DisableTimerJobIfEnabled -TimerJobName “timerjobname”

####### Modifies column title #####
#ModifyColumnName -sweb “root” -ListName “ListName” -field “eg-title” -value “value-replacing-title”
#ModifyColumnName -sweb “webname” -ListName “ListName” -field “eg-title” -value “value-replacing-title”
######### Add item to Link List ######################
#AddItemLinkList -sweb “Root” -desc “DescriptionText” -url “url of the page” -notes “notes for the url added”
#AddItemLinkList -sweb “webName” -desc “DescriptionText” -url “url of the page” -notes “notes for the url added”

 

 

Leave a Reply

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

Bitnami