S H A R E P O I N T C E N T E R

در این بخش 40 دستور مفید powershell شیرپوینت معرفی می‌شوند.

1. Create site collection using PowerShell in SharePoint

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables

$SiteCollURL = “https://www.enjoysharepoint.com/sites/RajDemo

$SiteName = “Welcome To Enjo SharePoint”

$SiteOwner = “HQAdmin\rswain”

$SiteTemplate = “STS#0” #Team Site Template

#Create new Site Collection

New-SPSite -URL $SiteCollURL -OwnerAlias $SiteOwner -Template $SiteTemplate -Name $SiteName

 

2. PowerShell create a site collection in a specific content database in SharePoint

New-SPSite -Name “Welcome To Enjo SharePoint” -ContentDatabase SP2013_demo_Content -url

https://www.enjoysharepoint.com/sites/RajDemo `-OwnerAlias “HQAdmin\rswain” -Template “STS#1”

 

3. Delete site collection PowerShell

Remove-SPSite -Identity “https://www.enjoysharepoint.com/sites/RajDemo

 

4. Force delete SharePoint site collection using PowerShell

$Site = Get-SPSite https://www.enjoysharepoint.com/sites/RajDem

$SiteContentDB = $site.ContentDatabase

$SiteContentDB.ForceDeleteSite($Site.Id, $false, $false)

 

5. PowerShell delete all site collections in a web application in SharePoint

$WebAppURL=”https://www.enjoysharepoint.com/sites/RajDemo

Get-SPWebApplication $WebAppURL | Get-SPSite -Limit ALL | Remove-SPSite -Confirm:$false

 

6. Bulk delete site collections with PowerShell

Get-SPSite “https://www.enjoysharepoint.com/sites/RajDemo” -Limit ALL | Remove-SPSite -Confirm:$false

 

7. PowerShell Get site information in SharePoint

Get-SPSite ‘https://www.enjoysharepoint.com/sites/RajDemo’ | Get-SPWeb -Limit All | Select Title

 

8. PowerShell get all site collections in a SharePoint farm

Get-SPWebApplication | Get-SPSite -Limit All | Format-Table -Property URL,ContentDatabase

 

9. Get all subsites from Site Collection using PowerShell

Get-SPWebApplication https://www.enjoysharepoint.com/sites/RajDemo | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select Title, URL | Export-CSV C:\SharePoint_Sites_Report.csv -NoTypeInformation

 

10. Get all subsites of a subsite using PowerShell in SharePoint

param ( [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$StartWeb, [Boolean]$IncludeStartWeb = $true )

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$subsites = ((Get-SPWeb $StartWeb).Site).allwebs | ?{$_.url -like “$StartWeb*”}

foreach($subsite in $subsites) { Write-Host $subsite.url }

 

11. SharePoint PowerShell backup content database

Backup-SPFarm -Directory <BackupFolder> -BackupMethod {Full | Differential} -Item <ContentDatabaseName> [-Verbose]

 

12. SharePoint PowerShell restore a content database

Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseName <DatabaseName> -DatabaseServer <DatabaseServer>

 

13. Backup site using PowerShell SharePoint 2013/2016/2019

Backup-SPSite https://www.enjoysharepoint.com/sites/RajDemo -Path C:\Backup\site_name.bak -UseSqlSnapshot

 

14. Restore site using PowerShell in SharePoint 2013/2016/2019

Restore-SPSite https://www.enjoysharepoint.com/sites/RajDemo -Path C:\Backup\site_name.bak -Force -DatabaseServer SQLBE1 -DatabaseName SQLDB1

 

15. Get all lists in a SharePoint Site using PowerShell

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$Site = Get-SPSite “https://www.enjoysharepoint.com/sites/RajDemo
# get the all sub sites of site
$SubSites = $Site.AllWebs
$SubSites | ForEach-Object {
$Site = $_
# get all lists from site
$lists = $Site.Lists | Where-Object { $_.BaseType -eq ‘GenericList’ }
$lists | ForEach-Object {
New-Object -TypeName PSObject -Property @{
ListName = $_.Title
SiteName = $Site.Title
SiteUrl = $Site.Url
}}}

 

16. Get all libraries in a SharePoint Site using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$Site = Get-SPSite “https://www.enjoysharepoint.com/sites/RajDemo
# get the all sub sites of site
$SubSites = $Site.AllWebs
$SubSites | ForEach-Object {
$Site = $_
# get all document Libraries from site
$lists = $Site.Lists | Where-Object { $_.BaseType -eq ‘DocumentLibrary’ }
$lists | ForEach-Object {
New-Object -TypeName PSObject -Property @{
LibraryName = $_.Title
SiteName = $Site.Title
SiteUrl = $Site.Url
}}}

 

17. Create a List using PowerShell in SharePoint

Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’ -ErrorAction SilentlyContinue
$SPweb = Get-SPWeb -Identity ‘https://www.enjoysharepoint.com/sites/RajDemo’
$ListTemplate = $SPweb.ListTemplates[‘Custom List’]
$SPweb.Lists.Add(‘SharePoint’, ‘List creation demo using PowerShell’ , $ListTemplate)

 

18. Delete List in SharePoint using PowerShell

Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’ -ErrorAction SilentlyContinue
$SPweb = Get-SPWeb -Identity ‘https://www.enjoysharepoint.com/sites/RajDemo’
$list = $SPweb.Lists[‘SharePoint’]
$item1 = $list.Items
$item1[0].Delete()

 

19. PowerShell add item to SharePoint list

Add-PSSnapin ‘Microsoft.SharePoint.PowerShell’ -ErrorAction SilentlyContinue
$SPweb = Get-SPWeb -Identity ‘https://www.enjoysharepoint.com/sites/RajDemo’
$list = $SPweb.Lists[‘SharePoint’]
$item1 = $list.Items.Add()
$item1[‘Title’] = ‘Chendrayan’
$item1.update()

 

20. Get list and libraries having versioning enabled in SharePoint using PowerShell

Get-SPWeb https://www.enjoysharepoint.com/sites/RajDemo |
Select -ExpandProperty Lists |
Where { -not $_.hidden -and
$_.EnableVersioning -eq $true} |
Select ParentWebUrl, title

 

21. Delete Item in a specific list in SharePoint using PowerShell

System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$site = new-object Microsoft.SharePoint.SPSite(“https://www.enjoysharepoint.com/sites/RajDemo“)
$relweburl = ”/rajtest”
$web = $site.openweb($relweburl)

$list = $web.Lists[“RajList”]

$listItems = $list.Items
$listItemsTotal = $listItems.Count

for ($item=$listItemsTotal-1;$item -ge 0; $item–)
{

Write-Host(“DELETED: ” )
$listItems[$item].Delete()
}
}

 

22. SharePoint PowerShell bulk delete list items

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
$web = get-spweb “https://www.enjoysharepoint.com/sites/RajDemo
$list = $web.lists[“List Title”]
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = “Scope=’Recursive'”
$query.RowLimit = 1000
$query.ViewFields = “<FieldRef Name=’ID’/>”
$query.ViewFieldsOnly = $true
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
Write-Host “Deleting Item – $($item.Id)”
$list.GetItemById($item.Id).delete()
}
}
while ($query.ListItemCollectionPosition -ne $null)

 

23. Activate features using PowerShell in SharePoint

Enable-SPFeature –Identity Reporting –url https://www.enjoysharepoint.com/sites/RajDemo

 

24. Deactivate features using PowerShell in SharePoint

Disable-SPFeature –Identity Reporting –url https://www.enjoysharepoint.com/sites/RajDemo

 

25. Get all features using PowerShell in SharePoint

Get-SPFeature –Site https://www.enjoysharepoint.com/sites/RajDemo

 

26. Add user to a SharePoint group using PowerShell

 #Get the Web
$web=Get-SPWeb “https://www.enjoysharepoint.com/sites/RajDemo
#Get the SharePoint Group
$Group= $web.Groups[“DHLOwners”]
$userName = “SPADmin\rswain”

#Add User to the site collection
$user = $web.EnsureUser($UserName)

#Add User to the Group
$group.AddUser($user)

 

27. Remove user from SharePoint group using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function RemoveUser-FromGroup($SiteURL, $GroupName, $UserAccount)
{
try
{
$ErrorActionPreference = “Stop”

#Get the Web
$web=Get-SPWeb $SiteURL

#Get the User to Remove
$User = Get-SPUser -Identity $UserAccount -Web $web

#Get the Group by its name
$Group = $Web.sitegroups | Where-Object {$_.Name -eq $GroupName}
if($Group -ne $null)
{
#sharepoint powershell delete user from group
$Group.RemoveUser($User)
Write-Host “$($User) Removed from the Group: $($GroupName)”
}
}
catch
{
#Write error message on screen and to a LOG file
write-host $_.Exception.Message
}
finally
{
$ErrorActionPreference = “Continue”
}
}

#Call the function to remove user from SharePoint group
RemoveUser-FromGroup “https://www.enjoysharepoint.com/sites/RajDemo” “DHL Owners” “SPADMIN\rswain”

 

28. Remove user from all SharePoint groups using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function RemoveUser-FromAllGroups($SiteURL, $UserAccount)
{
#Get the Web
$web=Get-SPWeb $SiteURL

#Get the User to Remove
$User = $Web.EnsureUser($UserAccount)

#Iterate through all Groups
foreach($Group in $Web.Groups)
{
$GroupUser = $Group.Users | where {$_.UserLogin -eq $User.UserLogin}
#Check if user member of the group
if($GroupUser -ne $null)
{
#remove user from sharepoint group using powershell
$Group.RemoveUser($User)
Write-Host “$($User) Removed from the Group: $($Group)”
}
}
}

#Call the function to remove a user from all groups in the site
RemoveUser-FromAllGroups “https://www.enjoysharepoint.com/sites/RajDemo” “SPADMIN\rswain”

 

29. Create a SharePoint group using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

#Custom Function to Create new SharePoint Group
function Create-SPGroup
{
param ($SiteURL, $GroupName, $PermissionLevel, $GroupDescription)

try
{
#Get the Web
$web = Get-SPWeb -Identity $SiteURL

if($web -ne $null)
{
#Check if Group Exists already
if ($web.SiteGroups[$GroupName] -ne $null)
{
write-Host “Group $GroupName exists Already!” -ForegroundColor Red
}
else
{
#Create SharePoint Group
$Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)
#Get the newly created group and assign permission to it
$Group = $web.SiteGroups[$groupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()

write-Host “Group: $GroupName created successfully!” -ForegroundColor Green
}

$web.Dispose()
}
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -ForegroundColor Red
}
}

#Call the function to create Sharepoint group
Create-SPGroup “https://www.enjoysharepoint.com/sites/RajDemo” “My Powershell Group”

 

30. Delete a SharePoint group using PowerShell

Add-PSSnapin “Microsoft.SharePoint.PowerShell”
#Get web
$spWeb = Get-SPWeb “https://www.enjoysharepoint.com/sites/RajDem“;
#Your group name
$GroupName=”My SharePoint Group”

if($spWeb.SiteGroups[$GroupName] -ne $null)
{
$spWeb.SiteGroups.Remove($GroupName)
$spWeb.Update()
Write-Host “Group Deleted!”
}
else
{
Write-Host “Group doesn’t Exists!”
}

 

31. Get all SharePoint users using PowerShell in SharePoint 2013/2016

function GetSPAllSPUsers($SiteCollectionURL,$SPListName)
{
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
$site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL)
$web = $site.openweb()
$list = $web.Lists[$SPListName]
$siteCollUsers = $web.SiteUsers

foreach($user in $siteCollUsers)
{
Write-Host ” ————————————- ”
Write-Host “Site Collection URL:”, $SiteCollectionURL
if($list.DoesUserHavePermissions([Microsoft.SharePoint.SPBasePermissions]::ViewListItems,$user) -eq $true)
{
Write-Host “User : “, $user.LoginName
Write-Host “Assigned Permissions : “, $list.GetUserEffectivePermissions($user.LoginName)
}
Write-Host ” ————————————- ”
}

$web.Dispose()
$site.Dispose()
}

 

32. Get all groups in SharePoint using PowerShell

$web = Get-SPWeb $webUrl;

$groups = $web.AssociatedGroups;

 

33. Change Site collection URL using PowerShell in SharePoint

$site = Get-SPSite https://www.enjoysharepoint.com/sites/RajDem
$site.Rename(“https://www.enjoysharepoint.com/sites/RajDemo1“)

 

34. Change site collection Title and Description using PowerShell SharePoint 2013/2016

function Using-Culture (
[System.Globalization.CultureInfo] $culture = (throw “USAGE: Using-Culture -Culture culture -Script {…}”),
[ScriptBlock] $script = (throw “USAGE: Using-Culture -Culture culture -Script {…}”))
{
$OldCulture = [Threading.Thread]::CurrentThread.CurrentCulture
$OldUICulture = [Threading.Thread]::CurrentThread.CurrentUICulture
try {
[Threading.Thread]::CurrentThread.CurrentCulture = $culture
[Threading.Thread]::CurrentThread.CurrentUICulture = $culture
Invoke-Command $script
}
finally {
[Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
[Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture
}
}

$web = Get-SPWeb “https://www.enjoysharepoint.com/sites/RajDem
$newTitle = “My new Title”
$newDesc = “My new Description”

# de-DE = German – Germany ; en-US = English – United States
Using-Culture de-DE { $web.Title=$newTitle; $web.Description=$newDesc; $web.Update() }

 

35. SharePoint PowerShell copy list items to another list

$WebURL = “https://www.enjoysharepoint.com/sites/RajDem
$SourceListName = “Source SharePoint”
$TargetListName= “Destination SharePoint”

#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]

#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();

#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
$TargetItem = $TargetList.AddItem()
Foreach($column in $SourceColumns)
{
if($column.ReadOnlyField -eq $False -and $column.InternalName -ne “Attachments”)
{
$TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
}
}
$TargetItem.Update();
}

 

36. Create a folder under a library in SharePoint using PowerShell

$webUrl = “https://www.enjoysharepoint.com/sites/RajDem
$listName = “My Parent List”
$numberFoldersToCreate = 4000;
$folderNamePrefix = “folder”;

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

# Create desired number of subfolders
for($i=1; $i -le $numberFoldersToCreate; $i++)
{
$folder = $list.AddItem(“”, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, “$folderNamePrefix$i”)
$folder.Update()
write-host $i
}

#Dispose web
$web.Dispose()

 

37. Change SharePoint authentication from classic mode to claims based mode using PowerShell

$WebAppName = “http://hqRajdev16
$wa = get-SPWebApplication $WebAppName
$wa.UseClaimsAuthentication = $true
$wa.Update()
$account = “TestAccount\Rajkiran”
$account = (New-SPClaimsPrincipal -identity $account -identitytype 1).ToEncodedString()
$wa = get-SPWebApplication $WebAppName
$zp = $wa.ZonePolicies(“Default”)
$p = $zp.Add($account,”PSPolicy”)
$fc=$wa.PolicyRoles.GetSpecialRole(“FullControl”)
$p.PolicyRoleBindings.Add($fc)
$wa.Update()
$wa.MigrateUsers($true)
$wa.ProvisionGlobally()

 

38. Get Service Applications using PowerShell in SharePoint

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

GET-SPServiceApplication | Format-Table {$_.DisplayName}

Once you run the above command it will display the service application list like below:

$_.DisplayName
————–
Access Services 2010
Secure Store Service
PowerPoint Conversion Service Application
State Service
Workflow Service Application
Project Server Service Application
PerformancePoint Service Application
Visio Graphics Service
Managed Metadata Service
App Management Service
Security Token Service Application
Machine Translation Service
Application Discovery and Load Balancer Service Application
Usage and Health data collection
Subscription Settings Service
Search Administration Web Service for Search Service Application
Word Automation Services
User Profile Service Application
Business Data Connectivity Service
Access Services
Search Service Application

 

39. How to Change SharePoint master page using PowerShell

 $web = Get-SPWeb http://win-pfcp2dgt8di/sites/EnjoySharePoint<br< a=””> />$web.CustomMasterUrl = “/_catalogs/masterpage/mycustom.master”
$web.MasterUrl = “/_catalogs/masterpage/mycustom.master”
$web.Update()</br<>

 

مطالب مرتبط

ارسال دیدگاه

آخرین نوشته ها