Windows Powershell in Action
Windows Powershell in Action
(Level:200)
Powershell Introduction
Variable & Object
Powershell Operator
Loop and Flow Control
Function and Debug
Powershell in Action
What is Powershell??
Discoverability
Consistency
Interactive and Scripting Environments
Object Orientation
Easy Transition to Scripting
Powershell fundamental
Revolutionary interactive shell and scripting
language
Based on .NET
New set of built-in tools (~130)
New language to take advantage of .NET
An “object-based” pipeline view
Can continue to use current tools
Can continue to use current instrumentation (COM,
ADSI, WMI, ADO, XML, Text, …)
Frequently Asked Questions
Do I need to learn .NET before I can use
Powershell?
No - you can continue to use existing tools
Do I need to rewrite all my existing tools?
No - existing tools will run just fine
Do I need to learn the new language?
No - You can easily run existing commands without
modification
Many Unix and DOS commands work… try them…
To begin working…
Variable Name
Variable Type and Type Adaptation
All Variables are Object
Array
Environmental Variables
Variable Name
$a=333
$b=“66”
$c=SS
$a.GetType()
$b.GetType().Name
$a+$b ; $b+$a ??
$b+$c ; $c+$b ??
$a+$c ; $c+$a ??
All Variables are Object
[int]$Age=22
$Age.GetType()
$Age GetType().Name
$Age | Get-Member
$Title=“manager”
$Title.length
$Title.CompareTo()
Array
Get-ChildItem Env:
Creating – and Modifying -- Environment
Variables
$env:testv = "This is a test environment variable.“
[Environment]::SetEnvironmentVariable("testv",
"VVVV", “User")
[Environment]::GetEnvironmentVariable(“testv","User
")
Remove-Item Env:\testv
[Environment]::SetEnvironmentVariable(“testv” ,
$null,"User")
PSDrive
PSDrive Operation
Get-PSDrive
mount -Name Seting -psProvider FileSystem
-Root "C:\Documents and Settings”
mount -Name MS -PSProvider Registry -Root
HKLM\Software\Microsoft
rdr -Name MS
Set-Location
Get-Location
Session 2 Ending
Demo, Q&A and Break
Powershell Operator
123+789 ; 222-876
34.5*44.2 ; 13/7
123%5
$var++ ; ++$var $var = $var + 1
$var-- ; --$var $var = $var – 1
Assignment Operators
$var=3
$var+=3 ; $var-=3
$var*=3 ;$var/=3 ; $var%=3
$var = 0x10 echo $var 16
$var = 7.56e3 echo $var 7560
$var=7MB echo $var 7340043 (bytes)
Logical Operators
Switch [-regex|-wildcard|-exact][-casesensitive]
-file <filename> (< variable >)
{
< Pattern 1> { code 1 }
< Pattern 2> { code 2 }
< Pattern 3> { code 3 } …
Default { code n }
}
ForEach ( Foreach-Object )
ForEach
($<item or object> in $<Collection object>)
{ <code> }
Script Block
Function
Function Arguments
Function Return Values
Variable Scope
Debug
Script Block
PS > $a = { $x = 2 , $y = 3 , $x * $y }
PS > &$a
PS > 6
PS > MySimpleFun
This is a function
Function Arguments
PS > AandB 8 2
10
6
16
ActiveDirectoryDemos
Get-DomainInfo
Get-HotFixes
Get-MachinesMissingHotfix
Get-SoftwareFeatures