Skip to main content


Showing posts with the label Powershell

Remove App from SharePoint Online with Powershell


Today I was facing some issues in installed custom app. I was not able to get App Details & also not able to remove the app. I tried alot from interface but I couldn't remove the app. Then I move to Powershell scripts.

Here is the powershell script which can remove the app from SharePoint Online. Only prerequisite is you must have SharePoint Online Client Components SDK installed on the system.


Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)
    return $context

Function Uninstall-AppInstance([Microsoft.SharePoint.Client.ClientContext]$Context,[Guid]$AppInstanceId)

Publish files which are not published by users

#Set-ExecutionPolicy Unrestricted
#Set-ExecutionPolicy RemoteSigned

$Host.UI.RawUI.WindowTitle = "-- Publish Files --"

$scriptdir = $PSScriptRoot

#$DatePrefix ="XML\"+ (Get-Date).ToString("MMddyyyyHHmm")
$DatePrefix ="XML\"

Write-Host "Loading the CSOM library" -foregroundcolor yellow

Add-Type -AssemblyName System.Web
# Paths to SDK. Please verify location on your c omputer.
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Taxonomy.dll"
Write-Host "Succesfully loaded the CSOM library" -foregroundcolor green

# Insert the credentials and the name of the admin site
$AdminPassword=Read-Host -Prompt "Password" -AsSecureString;


Remove Deleted Site Collections from SharePoint Online


Write-Host "Remove Deleted Site Collections"
Write-Host "Password Please :"

#Set-ExecutionPolicy RemoteSigned

$username = ""
$password=Read-Host -Prompt "Password" -AsSecureString;

$siteAdminURL = "<<Main Tenent URL Here>>"

Import-Module Microsoft.Online.SharePoint.PowerShell –DisableNameChecking

#$securePassword = ConvertTo-SecureString $password –AsPlainText –force


$O365Credential = New-Object System.Management.Automation.PsCredential($username, $securePassword)

Connect-SPOService -Url $siteAdminUrl -Credential $O365Credential

Get-SPODeletedSite | foreach {
Write-host "Deleting " $_.Url
Remove-SPODeletedSite –Identity $_.Url –Confirm:$false
Write-Host "Removed Site Collections"

Export ContentDataBase Name in csv file

<# Created by: Gaurav Goyal
   Purpose : To Export Content Database Name in csv file.
   Comments: Run With Admin Privileges \ Read operation.

SC $csv "ID,DBName,WebApplicationName,CurrentSiteCount"
write-host "`nGetting WebApplication Names`n";
$WebApps = Get-SPWebApplication 
foreach($WebAppUrl in $WebApps)
write-host "$i) Getting ContentDataBase name of WebApp"$WebAppUrl.Url "`n" ;$i++;
$toCSV=Get-SPContentDatabase -site $WebAppUrl.Url;
AC $csv"$($toCSV.Id),$($toCSV.Name),$($toCSV.WebApplication),$($toCSV.CurrentSiteCount)"
write-host "Exporting List of All DB.....`n"
Get-SPDatabase | epcsv ".\AllDataBaseList.csv"

New Site Collection Through PowerShell

#Creates a SPSite in a new content database
Add-PSSnapin Microsoft.SharePoint.Powershell - ErrorAction SilentlyContinue;

$siteName = "IT";
$webAppUrl = "";
$template = "STS#0";

$ownerAlias = "mydomain\ggoyal";
$secondaryOwnerAlias = "mydomain\ggoyal";

$siteUrl = "$webAppUrl/sites/$siteName";
$databaseName = "SharePoint_Intranet_$siteName";
$databaseServer = "Test-Server";

New-SPContentDatabase -Name $databaseName -DatabaseServer $databaseServer -WebApplication $webAppUrl;

New-SPSite -Url $siteUrl -OwnerAlias $ownerAlias -SecondaryOwnerAlias $secondaryOwnerAlias -ContentDatabase $databaseName -Template $template -Name $siteName;

# New SPSite does not create the default groups

$web = Get-SPWeb $siteUrl;
$web.CreateDefaultAssociatedGroups("i:0#.w|$ownerAlias", "i:0#.w|$secondaryOwnerAlias", $siteName);

Update Solution with Powershell

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$SolutionPackageName = "MySolution.wsp"
$SolutionPackagePath = "C:\PowershellSetup\MySolution.wsp"

Write-Host "Checking Solution exist or not..." -foregroundcolor Yellow
$solution = Get-SPSolution | where-object {$_.Name -eq $SolutionPackageName}
if($solution -ne $null)
Write-Host "Solution Found & Updating Solution..." -foregroundcolor Green
Update-SPSolution -Identity $SolutionPackageName -LiteralPath $SolutionPackagePath -Local -GACDeployment -Force
Write-Host "Solution Updated..." -foregroundcolor Yellow

Write-Host "Solution Updation Complete." -foregroundcolor Green

Install WSP with Powershell

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$SolutionPackageName = "MySolution.wsp"
$SolutionPackagePath = "C:\PowershellSetup\MySolution.wsp"

Write-Host "Checking Solution exist or not..." -foregroundcolor Yellow
$solution = Get-SPSolution | where-object {$_.Name -eq $SolutionPackageName}
if($solution -ne $null)
Write-Host "Solution Found..." -foregroundcolor Green
if($solution.Deployed -eq $true)
Write-Host "Uninstalling Solution..." -foregroundcolor red
Uninstall-SPSolution -Identity $SolutionPackageName -Local -Confirm:$false
Write-Host "Uninstallation Done..." -foregroundcolor Yellow
Write-Host "Removing Solution..." -foregroundcolor red
Remove-SPSolution -Identity $SolutionPackageName -Confirm:$false
Write-Host "Solution Removed..." -foregroundcolor Yellow

Write-Host "Adding Solution..." -foregroundcolor red
Add-SPSolution -LiteralPath $SolutionPackagePath
Write-Host &quo…

PowerShell Command to Export Sub Site

Export site with these option. This will include all customizations, user securities, list & libraries etc.
Export-SPWeb -Identity http://sharepoint-demo/IC -Path "c:\bkup\IC.cmp" -Force -IncludeUserSecurity -IncludeVersions All

Note : ·http://sharepoint-demo/IC : You can change this site url. Put url of site which is going to be backed up. ·C:\backup\IC.cmp : You can change backup path with file name. You have to keep extention .cmp as it is.

More information about “Export-SPWeb”

PowerShell Command to create new site collection

Create a Content Database
New-SPContentDatabase -Name IC_Content_DB -WebApplication http://sharepoint-demo

Note : “IC_Content_DB” :- Name of new content database for new Site Collection “http://sharepoint-demo” :-  Name of Web Application where you want to create new site collection with the url http://sharepoint-demo/IC
More information about “New-SPContentDatabase”

Now create a site collection
$template = Get-SPWebTemplate "STS#0" New-SPSite -Url "http://sharepoint-demo/IC" -OwnerAlias "sharepointdemo\gaurav" –ContentDatabase IC_Content_DB -Template $template
More information about “New-SPSite”