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”.

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.


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.

Set-Location -Path F:\PowershellScript
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

One script one pipeline

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


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:


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'"

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.


Learn basic windows powershell scripting tutorial for beginners administrators sharepoint 2019 admin #SharePoint2019, #PowerShell

Learn basic windows powershell scripting tutorial for beginners administrators sharepoint 2019 admin #SharePoint2019, #PowerShell


Categories: powershell, powershell command

Tags: , , , , , , , , , , , , , , , , , ,

%d bloggers like this: