Category Archives: powershell command

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
all-webs-and-site-templates-1920x1080

View all webs and site templates under site collection

View all webs and site templates under site collection

We can get all webs and site templates under site collection by using the below small script.

$site = Get-SPSite “http://win2016:37344/sites/SPmcse”
foreach ($web in $site.AllWebs){
$web | Select-Object -Property Title,Url,WebTemplate
}
$site.Dispose()
all-webs-and-site-templates-1317x299
maximum-number-of-site-collections-1920x1080

maximum number of site collections per content database

Set maximum number of site collections per content database

For Particular Requirement, client want to dedicate a database for a single site collection. When we create a site collection from central admin, site is placed automatically in any available content database. To prevent any other sites to be created on the particular content database, We can set maximum number of site collections on particular content database. Follow steps below :

  • Central Administration -> Application Management -> Management Content databases
  • Select our target web application in which the particular content database is attached
  • Pick the target database from the list
  • Now, in the “Manage Content Database Settings” page we can set the maximum number of sites for the content database.
maximum number of site collections
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for processing
$WebAppURL ="https://sharepointtechnicalsupport.com"
$MaxSiteCount=1
$WarningSiteCount = 0

#Get all content databases of the web application
$ContentDBColl = Get-SPContentDatabase -webapplication $WebAppURL

#Iterate through each database in the web application
foreach($Database in $ContentDBColl)
{
#Check the current No. of sites
if($MaxSiteCount -ge $Database.CurrentSiteCount)
{
#Set Maximum Sites, warning level Counts
Set-SPContentDatabase -Identity $Database.Name -MaxSiteCount $MaxSiteCount -WarningSiteCount $WarningSiteCount
Write-host "Max Sites Settings updated for the database:" $Database.name -ForegroundColor Green
}
else
{
write-host "MaxSiteCount must be > = current site count! No changes made in $($Database.Name)" -ForegroundColor Red
}
}