Monday, February 1, 2021

Powershell script for cleaning Recycle bin

In this blog post we will learn, how we can clean the recycle bin with PowerShell script. This is another way of cleaning Recycle bin apart from UI.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

We will use the below script to clean up the items from the recycle bin.


$webApp=get-SPWebApplication "http://sharepointblog"
foreach ($SPSite in $webApp.Sites) {     #Here we are looping through all the web under site collection     foreach($SPWeb in $SPSite.AllWebs)     {         #Cleaning the 1st Stage of Recycle bin items        $SPWeb.RecycleBin.DeleteAll();         #Moving the all the items to 2nd stage level         $SPWeb.RecycleBin.MoveAllToSecondStage();     }     #Clean SharePoint site collection     $SPSite.RecycleBin.DeleteAll(); }

Tuesday, January 26, 2021

Powershell script for creating SharePoint Site/Subsite

In this post we will learn how to create SharePoint site/sub site using PowerShell scripts. Again it is not mandatory to create this in PowerShell but its another way of creating it. We can definitely create it using UI as well.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

Below is the syntax and example to create the site using PowerShell script.

New-SPWeb _URL<Here we need to replace it with site URL> -name <Name of the sub site> -template <Site template need to be used while creating>

Example:
New-SPWeb –Url "http://blogspot/SubSite" -Name "Subsite" -template STS#0

Sunday, January 24, 2021

PowerShell script for creating site collection in Sharepoint

In this blog post we will know how to create new site collection in sharepoint (this applies for 2010,2013, 2016, 2019) using power shell scripts. Again we can create site collection using UI as well but here we will use PowerShell.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

Below is the standard template we will use while creating the site collection, we can replace it with custom values as per the requirement.

New-SPSite –Url <URL of the site collection>
-Name <Site collection name>
-OwnerAlias <Site collection administrator>
-Template <In which template site needs to be created like blog, team etc>
Here we are creating team site collection with with SPAdmin as the owner.

New-SPSite "http://blogpost/sites/test" -OwnerAlias "blogpost\spadmin" -Language 1033 -Template STS#0

PowerShell script for creating web application in Sharepoint

In this blog post we will use PowerShell script for creating new web application in SharePoint. We can create web application from SharePoint central admin site as well, this is another way of creating the web application.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

Below is the script for creating the application, here 1st script refers to generic script where you can replace with your own value as per requirement.

New-SPWebApplication –ApplicationPool <Application pool name> -Name <Web app name> -ApplicationPoolAccount <service account for the application pool> -Port <port number>

Below is the sample script for creating web app by internal host header with sharepoint.blogspot.com and public url with http://www.blogspot.com 

New-SPWebApplication -Name "BlogSite" -Port 80 -HostHeader sharepoint.blogspot.com -URL "http://www.blogspot.com" -ApplicationPool "SharepointBlogspot" -ApplicationPoolAccount (Get-SPManagedAccount "blogspot\spadmin")

We can create web application with claim based authentication enabled for that we need to use below script.

$authprovider = New-SPAuthenticationProvider
New-SPWebApplication -Name "BlogSite" -Port 443 -URL "https://www.blogspot.com" -ApplicaitonPool "SharepointBlogSpot" -ApplicationPoolAccount (Get-SPManagedAccount "blogspot\spadmin") -AuthenticationProvider $authprovider -SecureScoketLayer

Thursday, January 21, 2021

Powershell scripts for getting Sharepoint System account

In this post we will try to get all the service account details in SharePoint environment. Before proceeding further in case if you are new to PowerShell make sure you visit this blog post to understand how to set up or how to start using PowerShell ISE.

There is cmdlet called SPProcessAccount which helps in getting all the required account details.

Local System account details

Get-SPProcessAccount –LocalSystem



LocalService account:

Get-SPProcessAccount –LocalService




NetworkService account:

Get-SPProcessAccount –NetworkService




Tuesday, January 19, 2021

SharePoint build version using PowerShell

In this blog post will discuss about how to get the Sharepoint build version from PowerShell script.

Before that there are many ways we can get the Sharepoint build version, this is one of the reliable way to get the build version.

We can get build version using SQL queries but as per MS recommendation we should avoid connecting to Sharepoint DB, in a way we can avoid using this

Another way is using file system, again its not reliable about the build version we can get which version of Sharepoint like is it 2010, 2013 or etc. Using the Microsoft.Sharepoint.dll version number located in respective folder we can get but there will be always chance the number will vary depending Service Pack or CU updates

Using Registry we can find the build number but again this is not reliable as CU update doesn't reflect the build number in Registry hence we can't use this.

Another reliable way is using Sharepoint central administration, from their we will get the version exact version of the Sharepoint product installed.

As scope of this post is PowerShell we will use PS to get the Sharepoint installed version.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
get-spfarm | select BuildVersion

Or simply we can use

(Get-SPFarm).BuildVersion



PowerShell script for changing the site logo

In this post we will go through the PowerShell script for updating or I can say changing the sharepoint site logo. Even though its pretty simple activity which we can perform it from UI, but using PS script will make us more comfortable once we used to it.

Note: Before going through the please go through my first post in power shell series which convers how to use PS scripts.

Below is the very simple script for updating the logo of the site.

$web = Get-SPWeb "http://sharepoint2013”
$web.SiteLogoUrl = "http://sharepoint2013/publishingimages/logo.png"
$web.SiteLogoDescription = "Sharepoint 2013 Logo"
$web.Update()