0% found this document useful (0 votes)
7 views58 pages

Introduction to PowerShell Basics

Windows PowerShell is a command-line shell and scripting language developed by Microsoft, designed for system administrators to automate tasks and manage Windows environments. It operates on the .NET framework, allowing for object-based commands called cmdlets, which can handle complex tasks efficiently. While it offers powerful capabilities, it also has disadvantages such as requiring the .NET framework and potential security risks.

Uploaded by

muktansh saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views58 pages

Introduction to PowerShell Basics

Windows PowerShell is a command-line shell and scripting language developed by Microsoft, designed for system administrators to automate tasks and manage Windows environments. It operates on the .NET framework, allowing for object-based commands called cmdlets, which can handle complex tasks efficiently. While it offers powerful capabilities, it also has disadvantages such as requiring the .NET framework and potential security risks.

Uploaded by

muktansh saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT - 5

Prepared by, Param Ahir


WHAT IS POWERSHELL
What is Powershell
• Windows PowerShell is an automated task-based
command-line shell and associated scripting language
created by Microsoft.
• It is the new shell of Microsoft which combines the old
command prompt (CMD) functionality with a new scripting
instruction set with built-in system administration
functionality.
WHAT IS POWERSHELL
What is Powershell
• It is designed especially for the system administrators. Its
analogue in Linux OS is called as a Bash scripting.
• Unlike other shells, which accepts and return text, it is built
on the top of the .NET framework, CLR (Common
Language Runtime) and DLR (Dynamic Language
Runtime).
• It can accept and returns .NET Framework objects.
WHAT IS POWERSHELL
What is Powershell
• It helps IT professionals to control and automate the
administrations of the Windows operating system and other
applications which run on the Windows server environment.
• PowerShell enables administrators to perform tasks on
remote and local Windows system through full access to
COM and WMI.
• The commands in the Windows PowerShell are referred to as
cmdlets.
WHAT IS POWERSHELL
Why use Powershell?
• It is both a scripting language and a command-line Shell.
• It can interact with a different number of technologies.
• Windows PowerShell allows complete access to all the types in the .NET framework.
• PowerShell is object-based.
• Many interfaces of GUI that Microsoft designed for its various products are front end
interfaces to PowerShell.
• It is more secure than running VBScript or other scripting languages.
• It allows performing repetitive tasks more efficiently by combining multiple commands
and by writing scripts. Suppose, a system administrator wants to create hundreds of
active directory users, he can achieve this with the help of only some PowerShell
cmdlets placed in a script.
• Many complex and time-consuming configurations and tasks can be done in a second
with simple cmdlets of PowerShell.
WHAT IS POWERSHELL
Disadvantages of Powershell
• Framework: It requires .NET framework.
• Object-based: With most shells, the text-based commands are used to get the
work done while writing scripts. If a user switches to Windows PowerShell from
some other type of shells, he will have to get used to a different way of thinking.
Due to this, some users need more time to understand the PowerShell.
• Security risks: Another drawback of using PowerShell is that it can create some
potential security risks. Many professionals of IT use it as a way to connect
remotely to other servers and computers. During this process, PowerShell can
leave some holes open for security breaches. It is the major disadvantages of using
PowerShell script.
• Web Server: Another drawback of PowerShell is that it requires a user to run a
web server on his server when utilizing remote functionality.
WHAT IS POWERSHELL
WHAT IS POWERSHELL
WHAT IS POWERSHELL
Windows PowerShell ISE
• The Microsoft Windows PowerShell ISE is a graphical user interface-based

application and a default editor for Windows PowerShell.

• ISE stands for the Integrated Scripting Environment.

• It is an interface in which we can run commands and write, test, and debug

PowerShell scripts without writing all the commands in the command-line

interface.
WHAT IS POWERSHELL
Windows PowerShell ISE
The ISE window of PowerShell consists of following three panes:
• Script Pane: This pane allows the users to create and run the scripts. A user can
easily open, edit, and run the existing scripts in the script pane.
• Output Pane: This pane displays the outputs of the scripts and the commands
which are run by you. You can also clear and copy the contents in the Output
pane.
• Command Pane: This pane allows the users to write the commands. You can
easily execute the single or multiple-line command in the command pane.
WHAT IS POWERSHELL

Create and Run a PowerShell


Script
WHAT IS POWERSHELL
Enable PowerShell Scripts
There are four different types of execution policy in PowerShell:
• Restricted: In this policy, no script is executed.
• RemoteSigned: In this policy, only those scripts are run, which are downloaded
from the Internet, and these must be signed by the trusted publisher.
• Unrestricted:All the scripts of Windows PowerShell are run.
• AllSigned: Only those scripts can be run, which are signed by a trusted publisher.
• Command -
Set-ExecutionPolicy Unrestricted
WHAT IS POWERSHELL

Cmdlet
WHAT IS POWERSHELL
What is PowerShell Cmdlet?
• A cmdlet 'pronounced as a command-lets' is a lightweight
command which is used in the PowerShell environment.
• These are the special commands in the PowerShell
environment which implements the special functions.
• The cmdlets follow a 'verb-noun' pattern, such as 'set-
childItem'.
WHAT IS POWERSHELL
What is PowerShell Cmdlet?
• The cmdlets in a PowerShell perform an action and returns a
Microsoft .NET framework object to the next command in
the pipeline.
• Cmdlets can receive objects as input and can also output
their results as objects, which makes them suitable for use as
recipients in a pipeline.
WHAT IS POWERSHELL
Powershell Get-childItem
• The Get-ChildItem cmdlet gets the items and the child items in one or more locations. If an item is a
container, it gets the items inside the container, known as the child items.
• A location can be a registry hive, file system registry, or a certificate store.
• This cmdlet does not display the empty directories.
• The gci, dir, ls are the aliases for this cmdlet.
• Use the -Recurse parameter to get the items in all child containers and use the -Depth parameter to
limit the number of levels to recurse.
WHAT IS POWERSHELL
PowerShell Get-Item
• The Get-Item cmdlet gets the items at a particular location.
• This cmdlet does not get the content of item at the specified location unless we use a
'*' wildcard character to request all the content of the item.
WHAT IS POWERSHELL
PowerShell Get-Location
• The Get-Location cmdlet gets the information about the current working directory or a location stack.
• This cmdlet gets an object which represents the current directory, much like the print present working
directory (pwd) command.
• When we switch between the drives, PowerShell retains our location in each drive. We can also use this
cmdlet to find the location in each drive.
• This cmdlet is used to get the current directory at execution time and use it in scripts and functions, such as
in functions that display the current working directory in the PowerShell prompt.
• We can also use Get-Location cmdlet to view the location in the location stack.
WHAT IS POWERSHELL
PowerShell Set-Item
• The PowerShell Set-Item cmdlet changes the value of an item to the value specified in
the command.
• It changes the value of a variable or registry key.
• The Windows PowerShell FileSystem provider does not support this cmdlet.
• E.g. Set-Item –Path alias:ch –Value “C:\Program Files\Google\Chrome\
Application\[Link]”
WHAT IS POWERSHELL
PowerShell Set-location
• The Set-Location cmdlet sets the current PowerShell working location to a specified
location.
• The location could be a directory, a registry location, a subdirectory, or any provider
[Link] sl, cd, chdir are the aliases for the Set-Location cmdlet.
WHAT IS POWERSHELL
PowerShell New-Item
• The New-Item Cmdlet in PowerShell creates the files and folders in the file system and
also creates registry keys and entries in the registry.
• With the creation of a file, it also adds the initial content to the file.
• -ItemType, -Name
WHAT IS POWERSHELL
PowerShell Copy-Item
• The Copy-Item cmdlet copies an item form one location to another location within a
namespace.
• For instance, this cmdlet can copy a file to a folder, but it cannot copy a file to a
certificate drive.
• It does not delete or cut the items being copied.
• Those specific items that the cmdlet copy depend on the PowerShell provider, which
exposes that item.
• -Path and -Destination
WHAT IS POWERSHELL
PowerShell Move-Item
• The PowerShell Move-Item cmdlet moves an item from one location to another
location.
• It moves an item, including its contents, properties, and child items, from one location
to [Link] Same provider must support the locations.
WHAT IS POWERSHELL
PowerShell Remove-Item
• The PowerShell Remove-Item cmdlet deletes one or more specified items.
• Because many providers support this cmdlet so that it can delete many different types
of items, including files, folders, variables, registry keys, aliases, and functions.
WHAT IS POWERSHELL
PowerShell Rename-Item
• The PowerShell Rename-Item cmdlet renames an item in a PowerShell provider
namespace.
• It changes the name of a specified item. It does not affect the content of an item being
renamed.
• The ri, rm, rmdir, del, erase, and rd are the aliases of the Rename-Item cmdlet.
• -NewName
WHAT IS POWERSHELL
PowerShell Add-Content
• The Add-Content cmdlet appends the content to the specified item or a file, such as
adding words to the file.
• We can specify the content by typing it in the cmdlet or by specifying an object which
contains the content.
WHAT IS POWERSHELL
PowerShell Clear-Content
• The Clear-Content cmdlet deletes the content of an item, but it does not delete the
item, such as deleting the text from the file.
• As a result, an item exists, but that item is empty.
• This cmdlet works on an item with content, instead of items with values.
• The clc is the alias for this cmdlet.
WHAT IS POWERSHELL
PowerShell Get-Content
• The PowerShell Get-Content cmdlet gets the content of an item at a specified
location.
• The gc, cat, and type are the aliases of this cmdlet.
WHAT IS POWERSHELL
PowerShell Get-Date
• The Get-Date cmdlet in PowerShell displays the current date and time on the
PowerShell console.
• This cmdlet gets a DateTime object.
• We can also use the Get-Date to generate and send the date and time character string
to the other cmdlets or programs.
WHAT IS POWERSHELL
PowerShell Set-Content
• The PowerShell Set-Content cmdlet writes the new content or replacing the existing
content in a file. It differs from the Add-content cmdlet, which appends the content to
the file.
• To send the content to the Set-Content cmdlet, we can use the -Value parameter on
the command line, or we can send a content through the pipeline. The sc is the alias for
this cmdlet.
WHAT IS POWERSHELL
PowerShell Out-File
• The Out-File cmdlet in PowerShell sends the output to a specific file. When you need
to use its parameters, use this cmdlet instead of redirection operator (>).
WHAT IS POWERSHELL
PowerShell Write-Host
• The PowerShell Write-Host cmdlet is used to write the customized output to a host.
• We can specify the text color by using the -foreground parameter, and by using the -
background parameter, we can specify the background color.
WHAT IS POWERSHELL
PowerShell Get-Command
• The PowerShell Get-Command cmdlet is used to get all the commands which are
installed on the computer system.
• This cmdlet includes functions, other cmdlets, aliases, filters, scripts, and applications.
• We can get only those commands which have been imported into the current session
by using the -ListImported parameter.
WHAT IS POWERSHELL
PowerShell Get-Command
• The PowerShell Get-Command cmdlet is used to get all the commands which are
installed on the computer system.
• This cmdlet includes functions, other cmdlets, aliases, filters, scripts, and applications.
• We can get only those commands which have been imported into the current session
by using the -ListImported parameter.
WHAT IS POWERSHELL
PowerShell Variables
• A variable is a unit of memory in which the data is stored.
• In Windows PowerShell, the name of a variable starts with the dollar ($) sign, such as $process, $a.
• The name of the variables are not case-sensitive, and they include spaces and special characters.
• By default, the value of all the variables in a PowerShell is $null.
Delete a variable
• If you want to delete the value of the variable, use the clear-variable cmdlet, or change the value of it
to $null.
Type of a variable
• If you want to find the type of a variable, you can use the GetType() method.
WHAT IS POWERSHELL
PowerShell Variables
Variable Scope
• PowerShell variables can have a "scope" which determines where the variable is available.
• $[<scope-modifier>:]<name> = <value>
• Global: Global variables are those variables that are valid everywhere, even outside the scripts and
functions.
• Local:Those variables which can be created in a local scope. By default, the variable has a local scope.
• Script: Those variables which are created during the script. These variables are only available to that script in
which they are created.
WHAT IS POWERSHELL
PowerShell Variables
Types of Variables
• Following are the different types of variables in the Windows PowerShell:
1. User-Created Variables.
2. Automatic Variables.
3. Preference Variables.
4. User-created Variables
WHAT IS POWERSHELL
PowerShell Variables
User-created variables
• Those variables which are created and maintained by the user are called user-created
variables.
• The variables that we create at the PowerShell command line exist only while the
Window of PowerShell is open.
• When the Window of PowerShell is closed, the variables are also deleted.
• We can create the variables in the scripts with the local, global, or script scope.
WHAT IS POWERSHELL
PowerShell Variables
Automatic Variables
• Those variables which store the state of PowerShell are called automatic variables.
• The PowerShell creates this type of variable, and their values are changed by the
PowerShell to maintain their accuracy.
• The user cannot change the values of these variables.
• E.g., $pid, $Error, $pwd, $null, $Event
WHAT IS POWERSHELL
PowerShell Variables
Preference Variables
• Preference variables are those variables that store the user preferences for the
Windows PowerShell.
• The Windows PowerShell creates this type of variable, and they are populated with the
default values.
• Any user can change the value of preference variables.
• E.g., $MaximumHistoryCount, $OutputEncoding, $WarningPreference
WHAT IS POWERSHELL
PowerShell Variables
PowerShell Array

• An array in a PowerShell is a data structure that is used to store the collection of

values in a single variable.

• The values in an array can be the same type or the different types.

• A value can be a string, an integer, a generic object, or another array.

• E.g., $a = 1,2,3,4,5
WHAT IS POWERSHELL
PowerShell Variables
PowerShell Array
Size of an array
• An Array has a size according to the count of elements. You can get the size of an array
by using the following syntax:
• $variable_name.Length
• We can also add the value to the array by using the += operator. Use the following
syntax to add a value to an existing array:
• $variable_name += value
WHAT IS POWERSHELL
PowerShell Variables
PowerShell Array
Size of an array
• An Array has a size according to the count of elements. You can get the size of an array
by using the following syntax:
• $variable_name.Length
• We can also add the value to the array by using the += operator. Use the following
syntax to add a value to an existing array:
• $variable_name += value
WHAT IS POWERSHELL
PowerShell Variables
Initializing an empty array
We can initialize an empty array by using the following syntax:
$variable_name = @()
Remove element from an Array
E.g.
Suppose an array $array contain five elements 5,10,15,20,25. And you want to remove the
first and last value from an array, then use the following command:
$array = ($array[1] $array[2] $array[3])
WHAT IS POWERSHELL
PowerShell Variables
PowerShell Hast table
The PowerShell Hashtable is a data structure that stores one or more key/value pairs. It is also known
as the dictionary or an associative array.
Syntax
The following statement is the syntax to create the Hashtable:
$variable_name = @{ <key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>;}
The following statement is the syntax to create an ordered dictionary:
$variable_name = [ordered] @{ < key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>
WHAT IS POWERSHELL
PowerShell Operators
WHAT IS POWERSHELL
PowerShell Operators
WHAT IS POWERSHELL
PowerShell Operators
WHAT IS POWERSHELL
PowerShell Operators
WHAT IS POWERSHELL
PowerShell Operators
WHAT IS POWERSHELL
PowerShell Operators

PowerShell supports the following redirection operators:


1.>
2.>>
3.>&1
WHAT IS POWERSHELL
PowerShell Operators

PowerShell supports the following Split and Join Operators


-Join Operator
-Split Operator
WHAT IS POWERSHELL
PowerShell Conditional Statement
If Statement

If(test_expression) PS C:\> $a=16


{ PS C:\> $c=$a%2
Statement-1 PS C:\> if($c -eq 0)
>> {
Statement-2.......
>> echo "The number is even"
Statement-N
>> }
}
WHAT IS POWERSHELL
PowerShell Conditional Statement
If(test_expression) If-else Statement
{ PS C:\> $a=15
Statement-1 PS C:\> $c=$a%2
Statement-2....... PS C:\> if($c -eq 0)
Statement-N >> {
} >> echo "The number is even"
else >> } else
{ >> {
>> echo "The number is Odd"
Statement-1
>> }
Statement-2.......
Statement-N
}
WHAT IS POWERSHELL
PowerShell Conditional Statement
if (test_expression 1)
{ Else-if Statement
Statement-1
Statement-2....... PS C:\> $a=0
Statement-N
}
PS C:\> if ($a -gt 0)
else if (test_expression 2) >> {
{ >> echo "Number is positive"
Statement-1 >> } elseif($a -lt 0)
Statement-2.......
Statement-N >> {
} >> echo "Number is negative"
else >> } else
{
Statement-1
>> {
Statement-2....... >> echo " Number is zero"
Statement-N >> }
}
WHAT IS POWERSHELL
PowerShell Conditional Statement
Switch Statement
Switch (<test-expression>) PS C:\> $day=3
PS C:\> switch($day)
<condition1> { Code of Block-1 ; break } >> {
<condition2> { Code of Block-2 ; break } >> 1{echo "The day is Sunday"}
<condition3> { Code of Block-3 ; break } >> 2{echo "The day is Monday"}
. >> 3{echo "The day is Tuesday"}
. >> 4{echo "The day is Wednesday"}
>> 5{echo "The day is Thursday"}
.
>> 6{echo "The day is Friday"}
<condition3> {Code of Block-N ; break } >> 7{echo "The day is Saturday"}
>> }
WHAT IS POWERSHELL
PowerShell looping Statement
while Statement Do..while Statement Do..until Statement
while(test_expression) Do Do
{ {
{ Statement-1 Statement-1
Statement-1 Statement-2 Statement-2
Statement-2 Statement-N Statement-N
} while( test_expression) } until( test_expression)
Statement-N
}
WHAT IS POWERSHELL
PowerShell looping Statement
break Statement continue Statement
PS C:\> for($a=1; $a -lt 10; $a++) PS C:\> $a = 1
>> { PS C:\> while ($a -le 10)
>> if ($a -eq 6) >> {
>> if ($a -eq 5)
>> {
>> { $a += 1
>> break
>> continue
>> } >> }
>> echo $a >> echo $a
>> } >> $a += 1
>> }

You might also like