PowerShell Scripts for Sharepoint 2010 & 2013


Here are some useful Sharepoint PowerShell scripts :

Create a site 
New-SpWeb "http://sp2010/Site1" -Name "Site 1" -template "STS#0"

Delete a site
Remove-SPWeb "http://sp2010/Site1" -Confirm:$false

Create a List
$web=get-spweb -Identity "http://sp2010/Site1"
$web.Lists.add(<List Name>,<List Description>,<List Template>)

$list = $web.getList("http://sp2010/Site1/Lists/List1")
$FieldType = [Microsoft.Sharepoint.SPFieldType]::Text
$List.Fields.Add("Name",$FieldType,$false)


Delete a List 
$list = $web.getList("http://sp2010/Site1/Lists/List1")
$list.delete()

Add a list item
$item=$list.items.add()
$item["Title"] = "Your title here"
$item.update()

Update a list item
$item = $list.getItemById(1)
$item["Title"]="New Title"
$item.update()

Delete a list item
$item = $list.getItemById(1)
$item.delete()

Export a list to a CSV with PowerShell
$list.Items | Select Title | Export-csv C:\Test.csv

Import a CSV to a List with PowerShell
1.Create a script file and name it as 'Import.ps1"
2.Copy the following code into the script file

$web=get-spweb "http://sp2010/site1"
$list = $web.Lists["Students"]
$row=0

foreach($i in Import-Csv c:\test.csv)
{
$item=$list.items.add()
$item["Title"]=$i.Title 
$item["Name"]=$i.Name
$item.update()

$row++
}
$row.ToString() + " imported"

3.Save the script file.
4.Run the script from the management console.




Comments

Popular posts from this blog

Application Lifecycle Management - SDLC with ALM

8 Principles of Document Writing

Sharepoint 2010 : Crawl issues - Search refiners