Windows Windows Powershell Powershell Windows Windows Powershell Powershell
Windows Windows Powershell Powershell Windows Windows Powershell Powershell
Agenda
Introduction
PowerShell - Goals
Powershell – Basics
• CLI UI for MS
MS--DOS
• Default MS-
MS-DOS shell
• First program run after boot
• Executes AUTOEXEC.BAT configuration file
Introduction – cmd.exe
First CmdLets
CmdLets combination
Creating scripts
PowerShell – A bit of syntax (1)
• CmdLet syntax = <<verbverb>>-<noun
noun>>
e.g.: get-
get-help
• Parameters = – <parameter_name
parameter_name>>
e.g. : get
get--help –detailed
• Aliases : “get
“get--help
help”” or “help” or “man”
• Piping = <command1> | <command2>
e.g. get-
get-help * | get-
get-help –detailed
PowerShell – A bit of syntax (2)
• Output redirection :
e.g. get-
get-help * > c:/file.txt
• Wildcards :
* : zero or more characters - ( a* : an an,, anna
anna))
? : one character - (a? : an
an,, al, ai)
[a-
[a-z] : range of characters - (a[l
(a[l--n] : al,
al,am
am,,an)
an)
[bc
bc]] : specified characters - (a[
(a[lns
lns]] : al,an
al,an,,as)
as)
First CommandLets
• Get – <something
something>>
• Set – <something
something>>
->To Get or Set informations
informations,, properties
properties,, etc
• Import
Import––<something
something>>
• Export
Export––<something
something>>
-> To Import or Export objects from and into
specified file formats
CommandLets Combination
• Example :
Get-ChildItem – path
Get-
C:\\Users\
C: Users\Laptop\
Laptop\Desktop
CommandLets Combination
• Example :
Get-ChildItem – path
Get-
C:\\Users
C: Users\\Laptop
Laptop\\Desktop –recurse |
Measure--Object –Property Lenght –sum
Measure
CommandLets Combination
• Example :
(
Get-ChildItem -Path
Get-
C:\\Users\
C: Users\Laptop\
Laptop\Desktop -recurse |
Measure--Object -Property Length –sum
Measure
).sum / 1Gb
CommandLets Combination
• Example :
Get--ChildItem –Recurse
Get
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’}
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’} |
Measure--Object –Property Lenght
Measure
-Average
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’} |
Measure--Object –Property Lenght
Measure
-Average > result.txt
CommandLets Combination
• Example :
Get--Process wi*
Get
CommandLets Combination
• Example :
Foreach ( $_ in ( Get
Get--Process wi* ) )
CommandLets Combination
• Example :
Foreach ( $_ in ( Get-
Get-Process wi* ) )
{
If ( $_.Name –match ‘^w
‘^w’’ )
}
CommandLets Combination
• Example :
Foreach ( $_ in ( Get-
Get-Process wi* ) )
{
If ( $_.Name –match ‘^w
‘^w’’ )
{ $_.Name, $_.Cpu }
}
PowerShell – Creating Scripts
set-executionpolicy remotesigned
PowerShell Creating Scripts
• Digital Signature
• A script can be certified by
by::
– An authority
– Ourselves
Get-Help Set-AhutenticodeSignature
Working With Objects
.NET
Creating an Object
Exporting on XML
.NET
• $date = New
New--Object DateTime 2010,05,26
• $list = New-
New-Object
System.Collections.Generic.List[<
System.Collections.Generic.List [<Type
Type>]
>]
• $list_of_date = New
New--Object
System.Collections.Generic.List[[DateTime
System.Collections.Generic.List DateTime]]
Using .NET Objects
• $date = New
New--Object DateTime 2009,11,20
• $date2 = New
New--Object DateTime 2010,05,27
• $date3 = New
New--Object DateTime 2010,04,24
• $list_of_date.Add(($date
$list_of_date.Add $date))
• $list_of_date.Add($date2)
$list_of_date.Add ($date2)
• $list_of_date.Add($date3)
$list_of_date.Add ($date3)
Using .NET Objects
• myScript.ps1 | Export
Export--Clixml <FileName>
FileName>
• $variable = myScript.ps1
$variable | Export
Export--Clixml <FileName
FileName>>
• $myObject | Export
Export--Clixml <FileName>
FileName>
THAT’S IT!