gopas-goc-216-02-powershell-intro
gopas-goc-216-02-powershell-intro
2022
PowerShell introduction
Ing. Ondřej Ševeček | GOPAS a.s. |
MCSM:Directory2012 | MCM:Directory2008 | MVP:Enterprise Security | CEH |
CHFI | CISA |
[email protected] | www.sevecek.com |
History
▪ PowerShell 1.0
• must be downloaded as a beta version
▪ Windows PowerShell 2.0
• download for XP/2003/Vista/2008
• default in 7/2008R2
• installable in 8/2012/8.1/2012R2/10/2016/2019/2022 as a feature
▪ Windows PowerShell 3.0
• download for Vista/2008/7/2008R2
• default in 8/2012
▪ Windows PowerShell 4.0
• download for Vista/2008/7/2008R2
• default in 8.1/2012R2
▪ Windows PowerShell 5.0
• download for 7/2008R2/8/2012/8.1/2012R2
▪ Windows PowerShell 5.1
• download for 7/2008R2/8/2012/8.1/2012R2
• default in 10/2016/2019/2022
▪ PowerShell 7
• .NET Core
• download for 8.1/2012R2/10/2016/2019/2022
1
10. 1. 2022
2
10. 1. 2022
3
10. 1. 2022
PowerShell consoles
▪ Windows PowerShell
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ver 2
• version 2.0
• version 4.0/5.0/5.1 including 2.0 if installed
▪ Windows PowerShell additional installation folder
• some modules
%programfiles%\WindowsPowerShell
▪ PowerShell 7
%programfiles%\PowerShell\7\pwsh.exe
• newer version always deletes the previous folders
4
10. 1. 2022
$psVersionTable (PowerShell 7)
10
5
10. 1. 2022
write-host $_.name
} | select -unique
11
PowerShell ISE
▪ Windows PowerShell only
%windir%\System32\WindowsPowerShell\v1.0\powershell_ise.exe
12
6
10. 1. 2022
13
14
7
10. 1. 2022
Comments
<#
block comment
until the closing symbol
#>
15
Commands (executables)
# .exe found under one member of the %path% variable
ipconfig
ipconfig /all
ipconfig "/all"
ipconfig '/all'
16
8
10. 1. 2022
Commands (executables)
nltest /sc_verify:gps
17
18
9
10. 1. 2022
Commands (executables)
19
Commands (executables)
20
10
10. 1. 2022
Commands (cmdlets)
# invocation of a method from a netfx .DLL library
Get-Process
Get-Service ; Get-Volume
21
Commands (cmdlets)
# note the various parameter sets
Get-Help Get-Process
22
11
10. 1. 2022
Get-Date
Get-Date | fl *
23
Aliases
Get-ChildItem
dir
Get-Process
gps
Get-WmiObject Win32_LogicalDisk
gwmi Win32_LogicalDisk
cls
24
12
10. 1. 2022
[Text.Encoding]::ASCII.GetBytes('Hello world')
[IO.Path]::GetFileName('C:\temp\Training\starter.ps1')
25
26
13
10. 1. 2022
"hello world"
5.83
0x3E7
380MB
2GB
5.83 * 17.4
39 / 15
27
'Hello' * 20
# modulo
39 % 5
# boolean values
$true
$false
28
14
10. 1. 2022
(3..8)
29
$myAge = 42
$freeStorageOfMyNotebook = 17GB
$currentInterest = 5.83
$expressionResult = 38 - 11
$counter = 5
$counter ++
$counter = $counter + 3
30
15
10. 1. 2022
$myName = 'Kamil'
$myName
$myName = 5.83
$myName
$myName = Get-Process
$myName
31
32
16
10. 1. 2022
'Kamil is ' + 27
35.7 + '22.8'
33
# just no problem
$names[3]
34
17
10. 1. 2022
$cities['Stockholm']
$cities['Paris']
35
$students[1]
36
18
10. 1. 2022
$myName = 'Kamil'
$freeStorageOnMyNotebook = 17GB
37
$myName = 'Kamil'
$freeStorageOnMyNotebook = 17GB
38
19
10. 1. 2022
$myBrothersAge = 40
39
40
20
10. 1. 2022
$weAreInterestedIn = 'host'
Get-Process -Name svc$weAreInterestedIn
Get-Process -Nam "svc$weAreInterestedIn"
mkdir nameWith$folder
mkdir 'nameWith$folder'
41
42
21
10. 1. 2022
nltest /sc_verify:$([Environment]::UserDomainName)
nltest "/sc_verify:$([Environment]::UserDomainName)"
43
# true
17 -lt 39
# true
'ondrej' -eq 'ONdreJ'
# true, *?[h-m]
'Praha' -like 'p*a'
44
22
10. 1. 2022
$myAge = 26
$myBrothersAge = 22
$myHomeCity = 'Paris'
# true
($myAge -gt 20) -and ($myBrothersAge -gt 15)
# true
($myAge -gt 20) -or ($myBrothersAge -lt 60)
# true
$myHomeCity -ne 'Praha'
-not ($myHomeCity -eq 'Praha')
45
Binary operators
35 -bxor 119
119 -bxor 84
84 -bxor 35
46
23
10. 1. 2022
[string]::Format() operator
$myName = 'Kamil'
$freeStorageOnMyNotebook = 17GB
47
[string]::Format() operator
48
24
10. 1. 2022
case-insensitive
-eq -and logical and
equal
operator what
-ne not equal -or logical or
case-insensitive
-contains case-sensitive
array string search -ceq -not logical not
equal
case-sensitive
-ccontains case-sensitive
collection search -cne -xor logical xor
not equal
inverse -contains
-notcontains wildcard
collections/arrays -like -band binary and
* ? [k-p]
inverse -ccontains
-cnotcontains case-sensitive
collections/arrays -clike -bor binary or
wildcard
case-insensitive
-in
in a collection/array -match regex -bnot binary not
not a member of a
-notin case-sensitive
collection/array -cmatch -bxor binary xor
regex
case-sensitive
-cin, -cnotin
-in, -notin -gt greater than -f format string
-clt, -cgt, -cgt, case-sensitive
-cge string order -lt lower than -join join strings
case-sensitive
-le lower or equal -creplace
regex replace
49
& $validScript
50
25
10. 1. 2022
$error.Clear()
Get-WmiObject Non_Existent
39/0
$error
$error[1]
$env:WINDIR
$env:ProgramFiles
$env:LOGONSERVER
$env:TEMP
$psVersionTable
51
$myName = 'Kamil'
# formatted text with specific color
Write-Host ('My name is: {0}' -f $myName) -Foreground Green
52
26