Power Shell
Power Shell
Power Shell
Lab Setup
• Windows 10 Pro version
Getting Started with Powershell
What is Powershell
• PowerShell is a mixture of a command line, a functional programming
language, and an object-oriented programming language. PowerShell
is based on Microsoft .NET, which gives it a level of open flexibility
that was not available in Microsoft's scripting languages (such as
VBScript or batch) before this.
• PowerShell is an explorer's scripting language. With built-in help,
command discovery, and with access to much of the .NET
Framework, it is possible to dig down through the layers
Quick Reference
Powershell
Cheatsheet:
http://ramblingcookiemonster.github.io/images/C
heat-Sheets/powershell-basic-cheat-sheet2.pdf
Comments
Special Characters
Tick in PowerShell
• A tick may be used as a line continuation character. Consider the
following example:
Common Operators
Creating arrays and hashtables
Strings
Strings (contd)
Common reserved variables
Common reserved variables
Quick commands and hot keys
Powershell Playgrounds
• Powershell console
Console is the default Powershell Terminal
• Powershell ISE :
Windows PowerShell ISE . ISE stands for Integrated Scripting
Environment , and it is a graphical user interface that allows you to
easily create different scripts without having to type all the commands
in the command line
Command naming and discovery
• Commands in PowerShell are formed around verb and noun pairs in
the form verb-noun.
• Verbs :
• Get-Verb
• Complete list of Verbs https://msdn.microsoft.com/en-
us/library/ms714428(v=vs.85).aspx
• Nouns:
• The noun provides a very short description of the object the command is
expecting to act on.
• The noun part may be a single word, as is the case with Get-Process, New-
Item, or Get-Help or more than one word, as seen with Get-ChildItem,
Invoke-WebRequest, or Send-MailMessage.
Finding commands
• The verb-noun pairing can make it a lot easier to
find commands (without resorting to search engines).
Aliases
• An alias in PowerShell is an alternate name for a command. A
command may have more than one alias.
• The list of aliases may be viewed using Get-Alias
• Get-Alias dir
• Get-Alias -Definition Get-ChildItem
• An alias does not change how a command is used. There is no
practical difference between the following two following commands:
• cd $env:TEMP
• Set-Location $env:TEMP
• New aliases are created with the New-Alias
• “New-Alias grep -Value Select-String”
Parameters
• When viewing help for a command, we can see many
different approaches to different parameters.
• Optional parameters
• Optional parameters are surrounded by square brackets. This denotes
an optional parameter that requires a value (when used):
• SYNTAX
• Get-Process [-ComputerName <String[]>] ...
• Optional positional parameters
• It is not uncommon to see an optional positional parameter as the first parameter:
• SYNTAX
• Get-Process [[-Name] <String[]>] ...
• In this example, we may use either of the following:
• Get-Process -Name powershell
• Get-Process powershell
Mandatory parameters
• A mandatory parameter must always be supplied and is written as
follows:
• SYNTAX
• Get-ADUser -Filter <string> ...
• In this case, the Filter parameter must be written and it must be given
a value. For example, to supply a Filter for the command, the Filter
parameter must be explicitly written:
• Get-ADUser -Filter { sAMAccountName -eq "SomeName" }
Switch parameters
• Switch parameters have no arguments (values); the presence of a
switch parameter is sufficient; for example, Recurse is a switch
parameter for Get-ChildItem:
• SYNTAX
• Get-ChildItem ... [-Recurse] ...
• As with the other types of parameters, optional use is denoted by square
brackets.
• Switch parameters, by default, are false (not set). If a switch parameter is
true (set) by default, it is possible to set the value to false using the notation,
as shown in the following code:
• Get-ChildItem -Recurse:$false
Parameter values
• Value types of arguments (the type of value expected by a
parameter) are enclosed in angular brackets, as shown in the
following example:
• <string>
• <string[]>
• If a value is in the <string> form, a single value is expected. If the value is in
the <string[]> form, an array (or list) of values is expected.
• For example, Get-CimInstance accepts a single value only for the ClassName
parameter: Get-CimInstance -ClassName Win32_OperatingSystem
• Get-Process -Name powershell, explorer, smss
Confirm, WhatIf, and Force
• The Confirm, WhatIf, and Force parameters are used with commands
that make changes (to files, variables, data, and so on). These
parameters are often used with commands that use the verbs Set or
Remove, but the parameters are not limited to specific verbs.
• Confirm:
WhatIf
• By employing PowerShell, and appending the -WhatIf switch, you get
a preview of would happen without risking any damage.
Providers
• Providers in PowerShell present access to data that is
not normally easily accessible. There are providers for the filesystem,
registry, certificate store, and so on. Each provider arranges data so
that it resembles a filesystem.
Drives using providers
• The output from Get-PSProvider shows that each provider has one or
more drives associated with it.
• As providers are presented as a filesystem, accessing a provider is
similar to working with a drive. Let's look at the following example:
Modules
Powershell
What is module?
What is the PowerShell Gallery?
• In February 2016, Microsoft made the PowerShell Gallery public.
• The PowerShell Gallery may be searched
using https://powershellgallery.com
• Useful commands are Import-Module, Get-Module, Remove-Module,
Install-Module
Working with Objects in PowerShell
Pipelines
• The pipeline is used to send output from one command into another
command
• The object pipeline
• Languages such as Batch scripting (on Windows) or Bash scripting (ordinarily
on Linux or Unix) use a pipeline to pass text between commands. It is up to
the next command to figure out what the text means.
• PowerShell, on the other hand, sends objects from one command to another.
• The pipe (|) symbol is used to send the standard output between commands.
Members
• The Get-Member command
• The Get-Member command is used to view the different members of an
object. For example, it can be used to list all of the members of a process
object