$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();
}
Monday, February 1, 2021
Powershell script for cleaning Recycle bin
Tuesday, January 26, 2021
Powershell script for creating SharePoint Site/Subsite
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
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>
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()
Monday, January 18, 2021
Change sharepoint site title and Description using Powershell
Here I am adding few PowerShell scripts in the upcoming blog post which all Sharepoint developer/admin will be using frequently. In each post I will add only code snippet in powershell so it will be easy to learn and remember Ashwell.
In this post I will provide the script to change the site title and description of the site.
Before that I want to make sure I am using Windows powershell ISE to run the script. We have option to run the script from VS code also.
Default location of PowerShell ISE will be available at the below location:
<%SystemRoot%>\System32\WindowsPowerShell\v1.0\PowerShell.exe
The discussion we are using here is meant for sharepoint 2013/16/19 not with Sharepoint Online(there is different way of using powershell for online and it will be discussed later)
As soon as we install sharepoint in the server, Powershell cmdlets available in the Management shell, here we dont need to install any other package to start working with this.
Note: All the required PS(Powershell) cmdlets will be available in Microsoft.SharePoint.PowerShell.dll
We can connect to sharepoint site, either using Sharepoint Management shell or Windows Powershell ISE(WPISE), if we are using WPISE then we need to register Microsoft.SharePoint.PowerShell.dll module for SharePoint Server cmdlets other wise we can skip this part.
Steps to add Snapin:
Add-PSSnapin Microsoft.SharePoint.PowerShell
Note: This needs to be added at the starting of the script, so that we wont get any error while executing the scripts:
Also there is another way we can register this as below(this is kind of good practice to follow)
$spSnapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.PowerShell'}
if ($spSnapin -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
In the above script we are making sure if the snapin is already available or not.
Who can run the script:
User who is running the script should have securityadmin role membership in SQL box also user should be part of Admin group.
How to open Sharepoint Management shell/Power ISE?
Its always good practice to open/run the shell with administration mode(run as admin)
Below is the script used to change the title and description of the site.
$Web= Get-SPWeb "http://sp2013"
$Web.Title = "Sharepoint 2013 Site"
$Web.Description = "Team site for Development"
$web.Update()