Sharepoint Powershell Basis

To Enable SharePoint cmdlets in powerShell:

Add-PSSnapin "MIcrosoft.SharePoint.PowerShell"

1. Sample SharePoint cmdlets

Get-SPWebApplication => to displat all webapplication

Get-Command -PSSnapin "Microsoft.SharePoint.PowerShell" | format-table name =>show all commands

Get-SPSite "http://senthil-pc:8010/*"

Get-SPWeb -site http://senthil-pc:8010/sites/site2

Get-SPSite "http://senthil-pc:8010/*" | Get-SPWeb|Select Title

Get-SPUser -Web "http://senthil-pc:8010"

Get-SPFeature -Limit ALL | Where-Object {$_.Scope -eq "http://senthil-pc:8010"}

2. Create a web application

$name = "Web Application Created using Powershell"

$port = 20000

$url = "http://senthil-pc"

$appPoolName = "SP Default App Pool"

$appPoolAccount = Get-SPManagedAccount "senthil-pc\Administrator"

New-SPWebApplication -Name $name -Port $port -URL $url -ApplicationPool $appPoolName -ApplicationPoolAccount $appPoolAccount

3. Create a new Site Collection

$title = "Site Created using PowerShell"

$url="http://senthil-pc:20000"

$owner = "senthil-pc\senthil"

$template = "STS#0"

New-SPSite -URL $url -Name $title -OwnerAlias $owner -Template $template

4. Add Item in sharepoint List

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

# Script settings
$webUrl = "http://senthil-pc:8010"
$listName = "Test"
$numberItemsToCreate = 10
$itemNamePrefix = "License "

# Open web and library
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

# Create desired number of items in subfolder
for($i=1; $i -le $numberItemsToCreate; $i++)
{
$newItemSuffix = $i.ToString("00000")
$newItem = $list.AddItem()
$newItem["Title"] = "$itemNamePrefix$newItemSuffix"
$newItem.Update()
write-host "Item created: $itemNamePrefix$newItemSuffix"
}

#Dispose web

$web.Dispose()

5. Add attachment in list

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 function AddAttachment($item, $filePath)
 {
 $bytes = [System.IO.File]::ReadAllBytes($filePath)
 $item.Attachments.Add([System.IO.Path]::GetFileName($filePath),$bytes)
 $item.Update()
 }
 
 $site = [Microsoft.SharePoint.SPSite]("http://senthil-pc:8010")
 $web = $site.OpenWeb()
 $list = $web.Lists["Test"]
 $item = $list.GetItemById(1)
 AddAttachment $item "C:\Users\Senthil\Desktop\AA_v3.5.log"  

Comments

Popular posts from this blog

Sites.Selected | Graph API SharePoint Permission

Configure the SharePoint Online App Catalog

Azure Function | Sharepoint List item | Call from Power Automate Flow