Category Archives: powershell

Get content source in sharepoint 2019 search service application using powershell

Get content source in sharepoint 2019 search service application using powershell

Get content source in sharepoint 2019 search service application using powershell. follow the code below to get the list with Name, Id, Type, CrawlState, CrawlCompleted date and time

$ssa=Get-SPEnterpriseSearchServiceApplication
Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa
Get content source in sharepoint 2019 search service application using powershell
Advertisements
powershell basics information post 1

powershell basics information post 1

Users who want to learn powershell especially for beginners these powershell basics information is the first step to move forward. These powershell basic commands related information will be very helpful while running script.

How to run powershell script

If you are writing scripts and running script is not simply type the name of a script file. As shown in figure below, you can’t simply type the name of a script file in order to run it. Let’s say i am creating one script to get all services running in windows server.

create script in windows

Lets create a very basic and simple powershell command as script. Follow the step by step process how to create powershell script.

  • Open Notepad.
  • Type “Get-Service”.
  • Save note pad as “.ps1” extension say “GetService.ps1”.
run-powershell-script-1

Even with the execution policy configured to permit scripts, you must precede the script filename with a path. In figure below, you see the “.\ path (C:\Users\Administrator)” in use, which refers to the “current directory (PS C:\Users\Administrator)”. You could also provide any other absolute or relative path in order to run a script but you must provide a path of some kind.

run-powershell-script-2

The script I saved in the the path “C:\Users\Administrator” of my system, so when you are at path “.\ path (C:\Users\Administrator)” or “current directory (PS C:\Users\Administrator)” and type name of the script that you saved with say “.\get” in Windows PowerShell ISE, that will be reflected there which you can select and run.

Change Directory in PowerShell

When you are working in production server, all scripts must be stored in a specific folder from where you need to run. In this case you need to change directory while running. Follow the step by step process below so as to change directory in powershell. Let’s say script is stored in the folder “PowershellScript” so the “.\ path” will be “F:\PowershellScript“. you need to change directory by running the below command for running script.

change-directory-powershell-1
Set-Location -Path F:\PowershellScript
or
cd F:\PowershellScript
change directory powershell-2
change directory powershell-3

You can drag the script and place at “Windows PowerShell” window. Then press “Enter“.

running script current directory-2
Advertisements
Advertisements

One script one pipeline

open a console window and run the following, pressing Enter after each line:

Get-Service
Get-Process

Now type those exact same lines into a script file, or into the ISE’s script editing pane, and run the script. You’ll get different results.

In PowerShell, each time you hit Enter you start a new pipeline. Whatever commands you typed are run in that single pipeline, and at the end of the pipeline PowerShell converts the contents of the pipeline into a text display.

When you ran the two commands in the normal console, you did so in two distinct pipelines.Therefore, PowerShell was able to construct a unique display for each set of output. When entered into a script, however, both commands ran in the same pipeline and PowerShell formatting system isn’t sophisticated enough to construct the same unique output for two different sets of results.

Try running this in the console:

Get-Service;Get-Process

Those results should look the same as they did when you ran the script containing those two commands. That’s because in this case both commands ran in a single pipe-line—which is hat happened when you ran the script

PowerShell variables

Variables provide a named, temporary place in memory that can store objects whether those are simple values like the number 5 or a collection of complex objects like the output of Get-Service.

Think of variables as a box, into which you can put one or more things even dis-similar things. The box has a name, and in PowerShell that name can include almost anything. “Var” can be a variable name, as can “{my variable}“. In that second example, the curly brackets enclose a Variable name that contains spaces. As per best practice, stick with variable names that include letters, numbers, and underscores.

Using a variable’s name references the entire box, but if you want to reference the contents of the box you need to add a dollar sign: $var. Most commonly, you will see PowerShell variables preceded with the dollar sign because the whole point of using one is to get at the contents. It’s important to remember, however, that the dollar sign isn’t part of the variable name: It’s just a cue to tell PowerShell that you want the contents rather than the box itself.

#assigns a string object to the variable var
$var = 'hello'
#creates a variable with an integer
$number = 1
#creates an array, because PowerShell interprets all comma-separated lists as an array, or collection, of items
$numbers = 1,2,3,4,5,6,7,8,9

These examples show how to place items into a variable, by using the assignment operator (=).

One thing that can sometimes confuse newcomers is that PowerShell doesn’t understand any meaning you may associate with a variable name. A variable like $computername doesn’t tell the shell that the variable will contain a computer name. Similarly, $numbers doesn’t tell the shell that a variable will contain more than one number the shell doesn’t care if you use a variable name that happens to be plural. $numbers=1 is equally valid to the shell, as is $numbers=’fred’.

Variable names normally consist of just letters, numbers, and the underscore character. But ${thisisalsoalegalvariablename} is also a valid variable name. In this example, the curly brackets enclose the entire name. This is not recommend using that approach. it’s confusing to read, and there’s really no need to have such a long variable name.

When a variable does contain multiple values, you can use a special syntax to access just a single one of them. $numbers[0] gets the first item, $numbers[1] is the second,$numbers[-1] is the last, $numbers[-2] is the second to last, and so on.

Quotation marks

#insert a variable’s contents into a string
$name = 'Don'
#$prompt will now contain My name is Don because $name will be replaced with the contents of the variable
$prompt = "My name is $name"
$processes = Get-Process
$prompt = "The first process is using $($processes[0].vm) bytes of VM."
$debug = "`$computer contains $computer" #the first $ is escaped
$head = "Column`tColumn`tColumn" #`t is the tab character
$filter1 = "name='BITS'"
$computer = 'BITS'
$filter2 = "name='$computer'"
Advertisements
Advertisements

PowerShell object members and variables

Everything in PowerShell is an object. Even a string ‘name’ is also an object, of type System.String. We can pipe any object to Get-Member to see its type name (that is, the kind of object it is) as well as its members, which include its properties and methods.

$var = 'Hello'
$var | Get-Member
PowerShell object members and variables

Similarly we can check for objects of type integer.

Use a period after a variable name to tell the shell, “I don’t want to access the entire object within this variable; I want to access just one of its properties or methods.” After the period, provide the property or method name. Method names are always followed by parentheses (). Some methods accept input arguments, and those go within the parentheses in a comma-separated list. Other methods require no arguments, and so the parentheses are empty. But don’t forget the parentheses!

$svc = Get-Service
$svc[0].name         #get the first object's name property
$name = $svc[1].name
$name.length         #get the length property
$name.ToUpper()      #invoke the ToUpper method
PowerShell object members and variables 1
PowerShell object members and variables 2
PowerShell object members and variables 3

line 2 : Accessing the first item in the $svc variable. The period means “I don’t want that entire object—I just want a property or method.” We’ve then accessed just the name property. Line 5 illustrates how to access a method, by providing its name after a period, and then following that with the parentheses.

A period is normally an illegal character within a variable name, because the period means we want to access a property or method. That means line 2 below won’t work the way you might expect:

$service = 'bits'
$name = "Service is $service.ToUpper()"
$upper = $name.ToUpper()
$name = "Service is $upper"

On line 2, $name will contain Service is BITS.ToUpper() whereas on line 4 $name will contain Service is BITS.

Save list as template sharepoint 2019 powershell

Most of the cases end user or site owner who actually own the sharepoint site for production server, create list using out of box UI to create sharepoint list. There are certain cases where requirement is to save list as template and create another list using the saved template. Here i will share how to Save list as template sharepoint 2019 powershell. Creating powershell is very fast, easy, less performance issues compared to UI method.

I have a custom sharepoint list “Name: Report 2019”. The number of list items crossed the limit of listview threshold 5000. You got a request to create another list of similar template without including content. This case Save list as template sharepoint 2019 powershell would be helpful. Please find the script below.

Save list as template sharepoint 2019 powershell
#Configuration parameters
$WebURL="http://win-7uqhlrcrq7g:40331/sites/SpTechSupport/"
$ListName="Report 2019"
$TemplateName="Report 2019 Template"
$TemplateFileName="Report2019Template"
$TemplateDescription="Report 2019 List Template"
$SaveData = $true

#Get the Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]

#Save List as Template
$List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $SaveData)
Write-Host "List Saved as Template!"
Advertisements
Advertisements
Save list as template sharepoint 2019 powershell-2
Save list as template sharepoint 2019 powershell-2

Navigate to “List Template” and see, we save list as template.

Save list as template sharepoint 2019 powershell -3
check powershell version in windows server with powershell command

check powershell version in windows server with powershell command

This post describes how to check powershell version in windows server with powershell command. Many time we face issue while writing powershell script and facing powershell version compatibility issue. Powershell version check required at this moment.

$PSVersionTable
check powershell version in windows server with powershell command
sharepoint-content-database-1920x1080

content database in sharepoint

content database in sharepoint

A content database in sharepoint is not the database server itself but container for all the content for a single web application.

manage-content-databases-1277x640
content database in sharepoint

You can have only one content database for one web application however, you can separate content for multiple websites into multiple content databases for a site collection. You can also use a single content database for multiple site collections, keeping in mind that the site collection or collections represent a single web app.

we can run the powershell command as below to get quick details however can get from “Central Admin -> Application Management”  under “View all Site Collections”.

Get-SPContentDatabase
Get-SPContentDatabase-985x390
Get-SPContentDatabase-1898x653
Get-SPContentDatabase-1896x636

Isolation and sharing are expressed as the difference between one site collection using one database and numerous site collections sharing a database. The number of site collections using a database is also a scaling and performance issue. If you are deploying site collections with a high workload attached and greater expectancy for growth, make sure to use fewer site collections per database. You can also plan your content database strategy by adding databases to site collections as they grow or associate specific site collections only with specific content databases. The latter approach lets you isolate a database serving particular site collections from all the other databases and thus isolate the content it contains.

boundaries and limits content databases

There can be 500 content databases per farm at max. With 200GB data per content database and 100TB of data per farm. 60 million items including documents and list items. Number of site collections per content database recommended to 5,000. However, up to 10,000 site collections are supported.2500 non-Personal site collections and 7500 Personal Sites or 10000 Personal Sites alone. find in details from Microsoft Doc Link.

content database for each site collection powershell

We can run below powershell command to know which content database your site collection attached to.

$site = Get-SPSite "http://win2016:37344/sites/SPmcse"
write-host $site.WebApplication.ContentDatabases

or

$url="http://win2016:37344/sites/SPmcse"
(Get-SPSite $url).ContentDatabase.name
Get-SPContentDatabase-1920x1303
Get-SPContentDatabase-1048x480
Service-Applications-in-SharePoint-1920x1080

Service Applications in SharePoint

Service Applications in SharePoint

Service Applications in SharePoint are associated with web applications, and specific services are typically deployed as needed in a particular farm. Only deployed services are referred to as service applications. This is a huge advantage in terms of conserving resources and optimizing performance. For instance, a specific web application can be configured to use only the services it needs. The number of service applications that exist is vast, and, as third-party vendors can create their own services for SharePoint Server. A list of services includes the following:

You can set up a single service application to be shared among multiple web applications or deploy multiple instances of the same service across a farm and, in some cases, across multiple farms. Also, there is no limitation regarding the number of services that can be deployed in any single farm. A typical planning scenario requires that you either set up services to share across multiple web applications or isolate an individual service to one or a limited number of web applications. We can publish below service applications across the farm.

  • Business Data Connectivity
  • Machine Translation
  • Managed Metadata
  • User Profile
  • Search
  • Secure Store

Navigate to “Central Admin -> Application Management ->Manage Service Applications” and click on “New” from ribbon to see the list of service applications available.

Service-Applications-in-SharePoint-1300x674

you can use powershell command to get the list of  service applications

Get-SPServiceApplication | Format-Table -wrap
Service Applications in SharePoint
Get-SPServiceApplication
Get-SPServiceApplication -Identity 48fbecaa-71e4-4f1d-bdfe-bdcbc17fa061 | Format-Table -wrap
Get-SPServiceApplication-1324x186

sharepoint services 2016

For every service application there is a service associated with it which you can from central admin by navigating to “Central Admin -> Application Management -> Manage Services on Server”.

manage-services-on-server-1273x441
services-on-server-1324x870

Alternatively you can use the powershell command as below

Get-SPServiceInstance | Format-Table -wrap
Get-SPServiceInstance-873x1099
Get-SPServiceInstance | Where {$_.Status -eq "Online"} | Format-Table -wrap
Get-SPServiceInstance-formt-1154x933

Similarly we can start the service instance by running the below command.

Start-SPServiceInstance e63962e4-e1ea-4b83-a9cc-df51683e85f3