SharePoint & PowerShell

Windows PowerShell is a shell developed by Microsoft for purposes of task automation and configuration management. This powerful shell is based on the .NET framework and it includes a command-line shell and a scripting language. In fact, it’s the native Windows management shell, not a technology that’s unique to SharePoint.

In PowerShell you cannot perform any SharePoint task because that snap-ins is not loaded.

We can perform each and every operation (which is provided by SharePoint Management Shell) in Windows Management Shell by adding snap-in of Microsoft.SharePoint.PowerShell.

Of course we can use all commands in SharePoint Management Shell

To load the SharePoint snap-ins in Windows Management Shell, you must run the following command:


Add-PSSnapin Microsoft.SharePoint.PowerShell

Once SharePoint snap-ins are added into PowerShell, we can use PowerShell as SharePoint Management Shell. We are able to execute each and every command which we can execute in SharePoint Management Shell.

Execute SharePoint Command in PowerShell after adding SharePoint snap-in.

As shown in image ,command Get-SPWebApplication Returns all Web applications that match the given criteria.

We can also remove snap-in from PowerShell by following command.


REMOVE-PSSNAPIN "Microsoft.SharePoint.Powershell"

It will remove SharePoint snap-in and we are not able to access any SharePoint Command

Get-SPSite

Get-SPSite "http://spserver:2222/*"

Get-SPSite: Returns all site collections that match the specified criteria.

Get-SPWeb:

Get-SPWeb -site "http://spserver:2222/"

Get-SPWeb: Returns all subsites that match the given criteria.

Get-SPSite with Select Parameter

Return selected column name

Get-SPSite "http://spserver:2222/*" |Get-SPWeb | Select Title 

If we remove, select it will return with Site URL

Get-SPSite "http://spserver:2222/*" |Get-SPWeb | Select

Get-SPUser

Get-SPUser: Returns the user account or accounts that match a given search criteria.

Get-SPUser -Web "http://spserver:2222/"

Get-SPFeature

Get-SPFeature: Returns the SharePoint Features based on a given criteria.

Here Get-SPFeature returns Features whose scope is Site

Get-SPFeature -Limit ALL | Where-Object {$_.Scope -eq "SITE"}

Create Web Application

New-SPWebApplication: Creates a new Web application within the local farm.

$name = "Publishing Web Powershell"
$port = 3355
$url = "http://SPServer"
$appPoolName = "Publishing Site Pool Name"
$ContentDatabase = "PublishingSiteDB"
$appPoolAccount =Get-SPManagedAccount "NRWebs\spadmin"
$ap = New-SPAuthenticationProvider
New-SPWebApplication -Name $name -Port $port -DatabaseName $ContentDatabase -URL $url -ApplicationPool $appPoolName -ApplicationPoolAccount $appPoolAccount -AuthenticationProvider $ap 


After successfully execution of command we can see newly created web application in SharePoint Central Administration.

It will also creates Application Pool in IIS.

Now Next let’s create Site Collection in Web Application.

New-SPSite : Creates a new site collection at the specified URL.

##
#Create Site Collection
##
$title = "SharePoint Publishing Site"
$url ="http://SPServer:3355"
$owner = "NRWebs\spadmin"
$template = "BLANKINTERNET#0"
New-SPSite -URL $url -Name $title -OwnerAlias $owner -Template $template


Once Site Collection is created we are able to review it in Central Administration in Application Management -> Site Collections -> View All Site Collection

Now we are able to browse Site.

In above command we have used Publishing Site Template to create Site Collection you can create your desired site by using its Template ID
You can get other template Id using following command.
Get-SPWebTemplate :
Displays all globally installed site templates that match the given identity

Get-SPWebTemplate


Create SubSite
New-SPWeb: Creates a new site in an existing site collection.

##
#Create Sub Site
##
$template = GET-SPWebTemplate "BLOG#0"
New-SPWeb http://SPServer:3355/MyBlog -Template $template


Once Subsite is created we can browser it using its url.


Leave a comment