Getting started with Power Shell Scripting Ravikanth C



















- Slides: 19

Getting started with Power. Shell Scripting Ravikanth C Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

About me �A lead engineer at Dell � Share. Point professional by day and Power. Shell scripter at night � Blog at http: //www. ravichaganti. com/blog � Developer of ◦ ◦ PSCode. Plex PSRemote. File. Explorer Remote File Explorer Power. Pack BITS File Transfer Power. Pack � Author of ◦ Free e. Book - Layman’s guide to Power. Shell 2. 0 remoting (Coming soon) Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Agenda � What is Power. Shell? � Getting started � Using Power. Shell � Getting Help � Tools for the job � Learning resources �Q & A Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Show me the Power first. ! � Remote file explorer -> 698 lines of Power. Shell � This is just one example. . ! Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

What is Power. Shell? � The best shell scripting environment for Windows ◦ A new scripting language ◦ Replacement for a weak Windows command-line � Default management / automation platform for all Microsoft products going forward � Object based ◦ Built on top of. NET type system DOS Batch ◦ Everything is an object Power. Shell @echo off set. Local Enable. Delayed. Expansion set /a value=0 set /a sum=0 FOR /R %1 %%I IN (*) DO ( set /a value=%%~z. I/1024 set /a sum=!sum!+!value! ) @echo Size is: !sum! k Blog: http: //www. ravichaganti. com $size=0; Get-Child. Item C: Scripts | % { $Size+=$_. Length} Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Getting started � Power. Shell is in version 2. 0 � Default on Windows 7 and Windows Server 2008 � Part of Windows management framework download for Windows Server 2008, Vista, XP and Windows 2003 ◦ http: //support. microsoft. com/kb/968929 � Requires Blog: http: //www. ravichaganti. com . NET 2. 0 SP 1 or later Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Console � Console history, Intellisense for cmdlets, variables and parameters Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: ISE Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Cmdlets � Pronounced as command-let � A lightweight command � an instance of. NET framework classes � Use verb-noun pairs ◦ Get-Verb to list all approved verbs Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Variables � Not case-sensitive � Default value is $null � Cmdlets to manage variables ◦ ◦ ◦ Clear-Variable Get-Variable New-Variable Remove-Variable Set-Variable Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Variables � Types ◦ User created variables ◦ Automatic variables �Created by Power. Shell to maintain its state �Cannot be modified �Examples: $PSHome, $$, $^, $? , etc ◦ Preference variables �Created by Power. Shell to store user preferences �Can be modified �Examples: $Maximum. History. Count, etc Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Aliases � Alternate names for cmdlets � Cmdlets to manage aliases ◦ ◦ ◦ Export-Alias Get-Alias Import-Alias New-Alias Set-Alias � Cannot take parameters ◦ Workaround: put the statement in a script and alias to that � Example: Blog: http: //www. ravichaganti. com Dir, ls, gcm, gci, etc Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Conditional & Looping � Conditional ◦ If ◦ Switch statements � l. ooping ◦ For �For ($i=0; $i –le 10; $i++) {Write-Host $i} ◦ Foreach �Used on a collection of items �Foreach ($name in $names. Array) { Write-Host $name } ◦ Do �Do { Write-Host $i; $i++ } while ($i –le 10) �Do { Write-Host $i; $i++} Until ($i –lt 10) ◦ While �While ($i –le 10) { Write-Host $i; $i++ } Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Operators � Comparison operators ◦ -ne, -eq, -lt, -ge, -like, -notlike, -contains, notcontains, -match, -notmatch � Logical operators ◦ -or, -and, -xor, -not, ! � Arithmetic operators � Assignment operators Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Using Power. Shell: Arrays & Hash Tables � Arrays ◦ Collection of objects of the same type ◦ $a = @( ) ◦ $a = “Power. Shell”, ”VBScript” � Hash Tables ◦ Collection of key value pairs ◦ $ hash = @{ } ◦ $a = {“Name”=“Power. Shell”; “Is. Hot”=$true} Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Getting Help � Get-Command ◦ Gets a list of all commands � Get-Help ◦ Shows help for a given cmdlet � Get-Member ◦ Gets members of an object � Get-PSDrive ◦ Shows information stores in Power. Shell � About_Topics ◦ Get-Help about* Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Best practices � In Power. Shell, there will be more than one way to do the same thing. For example ◦ [System. Diagnostics. Process]: : Get. Processes() ◦ Get-Process � Understand execution policies � Use Power. Shell profiles Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Tools for the job � Power. Shell Console � Power. Shell ISE � Power. GUI Admin. Console � Power. GUI Script. Editor � Power. WF � Power. Shell Plus Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com

Learning resources � Getting started guide � Power. Shell Learning center � Free Power. Shell e. Book � The scripting Guys blog � Power. Scripting Podcast � @PSBUG on Twitter Blog: http: //www. ravichaganti. com Twitter: @Ravikanth Email: Ravikanth@Ravichaganti. com