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
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“
$Site = Get-SPSite https://www.enjoysharepoint.com/sites/RajDem
$SiteContentDB = $site.ContentDatabase
$SiteContentDB.ForceDeleteSite($Site.Id, $false, $false)
$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
Get-SPSite ‘https://www.enjoysharepoint.com/sites/RajDemo’ | Get-SPWeb -Limit All | Select Title
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
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 }
Backup-SPFarm -Directory <BackupFolder> -BackupMethod {Full | Differential} -Item <ContentDatabaseName> [-Verbose]
Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseName <DatabaseName> -DatabaseServer <DatabaseServer>
Backup-SPSite https://www.enjoysharepoint.com/sites/RajDemo -Path C:\Backup\site_name.bak -UseSqlSnapshot
Restore-SPSite https://www.enjoysharepoint.com/sites/RajDemo -Path C:\Backup\site_name.bak -Force -DatabaseServer SQLBE1 -DatabaseName SQLDB1
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
}}}
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
}}}
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)
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()
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()
Get-SPWeb https://www.enjoysharepoint.com/sites/RajDemo |
Select -ExpandProperty Lists |
Where { -not $_.hidden -and
$_.EnableVersioning -eq $true} |
Select ParentWebUrl, title
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()
}
}
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)
Enable-SPFeature –Identity Reporting –url https://www.enjoysharepoint.com/sites/RajDemo
Disable-SPFeature –Identity Reporting –url https://www.enjoysharepoint.com/sites/RajDemo
Get-SPFeature –Site https://www.enjoysharepoint.com/sites/RajDemo
#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)
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”
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”
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”
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!”
}
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()
}
$web = Get-SPWeb $webUrl;
$groups = $web.AssociatedGroups;
$site = Get-SPSite https://www.enjoysharepoint.com/sites/RajDem
$site.Rename(“https://www.enjoysharepoint.com/sites/RajDemo1“)
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() }
$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();
}
$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()
$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()
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