2014
Introduction to PowerShell
MICHAEL BLUMENTHAL
PSC GROUP, LLC
2014
Who is Michael Blumenthal?
• PSC Group
• Sr. Solution Architect
• SharePoint Architect
• Speaker
• CSPUG Co-Leader
• 19 years in IT Consulting
• 7 years working with PowerShell
2014
Why PowerShell?
Command Line Control of
Product!
MICROSOFT
2014
The Road To Power (Shell)
• It’s Easy to Get Started!1
• Learn the PowerShell Syntax2
• Real World Examples3
• Best Practices4
• More Resources5
2014
Chapter 1
2014
Getting Started with PowerShell
2003
2008, R2,
2012, R2
7, 8, 8.1, +
2014
Windows Feature
2014
2014
PowerShell V3&4 ISE
2014
Chapter 2
2014
Symbols, Keywords, and Syntax! Oh My!
• Variables1
• Commands2
• Piping3
• Comparisons4
• Flow Control5
• Filtering6
2014
Punctuation Pronunciation
$ _ : %
# [ ] ( ) ;
| . + =
{ } < > ! /, 
“ -
2014
Pop Quiz…
1$#|
2014
Variables
• Case Insensitive, Dynamic typing
$foo
$true, $false, $profile, $null
$foo = “Hello, World”
1
2014
2014
Commands are called cmdlets.
Verb-Noun
Built-in, Extensible
Get-Help & Help
Get-Member
2
2014
Help!
2014
The Power of Piping!
Output Of
Command 1
Input of
Command 2
3
Example
2014
Dial zero for an…4
Operator
-eq -le
-ne -like
-gt -notlike
-ge -match
-lt -notmatch
2014
Example
2014
Taking Control of the Flow
• For (Init;Test;Repeat) {Commands}
• for($i=1; $i -le 10; $i++) {Write-Host $i}For
• Foreach (Item in Collection) {Commands}
• Foreach ($web in $site.AllWebs) {$web.Title}ForEach
• If (Test) {Commands}
• if ($web.Title –ne “”) {Write-Host $web.Title}If
• While (Condition){Commands}
• while($val -ne 3){$val++; Write-Host $val}While
5
2014
Example
2014
Where-Object
•Where {<Test>}Syntax
• V1&2:Dir | Where {$_.Name –like
“B*”}
• V3:Dir | where Name –like B*
Example
6
2014
Executing Scripts
.filename.ps1
Set-ExecutionPolicy
Unrestricted
2014
2014
Chapter 3
2014
Real World Examples
• Dell Service Tag
• Audio Alerts
• Flash cards
• MP3, MP4 Management
• Managing Microsoft Server Products
• File Conversion & Text Manipulation
2014
Get-DellServiceTag
• Get-WmiObject win32_SystemEnclosure | select serialnumber
2014
Audio Alerts
• Stick this at the end of your long running script:
$Voice = new-object -com SAPI.SpVoice
$Voice.Speak(“Deployment is done!")
2014
Flash Cards
2014
Managing Video
Files
2014
File and Text Wrangling
• Word
• Auto
DOCX
• RegEx
• PSObj
TXT
• Export-
• CSV
CSV
2014
Chapter 4
2014
Comment your functions
• <#
• .SYNOPSIS –a brief explanation of what the script or function does.
• .DESCRIPTION – a more detailed explanation of what the script or function
does.
• .PARAMETER name – an explanation of a specific parameter. Replace name
with the parameter name. You can have one of these sections for each
parameter the script or function uses.
• .EXAMPLE – an example of how to use the script or function. You can have
multiple .EXAMPLE sections if you want to provide more than one example.
• .NOTES – any miscellaneous notes on using the script or function.
• .LINK – a cross-reference to another help topic; you can have more than one
of these. If you include a URL beginning with http:// or https://, the shell will
open that URL when the Help command’s –online parameter is used.
• #>
2014
Find custom commands this way
Refresh the command list
Actions you can take once
you fill in parameters
2014
Self Announcing Functions
…
2014
Source Code Control
2014
More Good Ideas
• Always read scripts before running them
• Make yours safe when others don’t
• Check for valid parameter values
• get-help
about_Functions_Advanced_Parameters
• Do error handling
• get-help about_Try_Catch_Finally
• get-help about_CommonParameters
• -ErrorAction and -ErrorVariable
2014
Demo Time!
2014
Chapter 5
2014
2014
2014
2014
2014
Resources Summary
 PowerShell Product
Team Blog
 TechNet Script Center
 Manning Press Books
 Blog.BlumenthalIT.Net
• Jeff Hicks
• POSHCODE.ORG
• SPYam
• Michaelblumenthal.me
2014
Script something today!
It’s Easy to Get
Started!
Learn & Use the
PowerShell Syntax
More Resources
In Review…
2014
Raffle
• Two PowerShell E-books courtesy of Manning Press
• Windows PowerShell in Action, Second Edition
• PowerShell in Depth, Second Edition
2014
Questions
• Michael Blumenthal
SharePoint Architect
PSC Group, LLC
• MBlumenthal@psclistens.com
• psclistens.com
• www.cspug.org
• Twitter: @MichaelBL
• LinkedIn
• Microsoft Midwest
Customers Yammer Network
Thank you for your time today.

Introduction to PowerShell - Presented to the SDC Nov 3, 2014

Editor's Notes

  • #4 .NET Every Server Product Every OS
  • #5  Who would like another tool in their toolbox? How about the swiss army knife of tools? It will make you a better developer!
  • #7 Who here already has installed and PowerShell? Win 2003: http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Win 2008 – It’s an OS Feature – “servermanagercmd –install PowerShell” Win2008R2 – it’s already installed
  • #14 Does anyone have any other names for these? Victor Borge has his own take on these: http://www.youtube.com/watch?v=lF4qii8S3gw $ Dollar sign, money _ Underscore # Hash, Pound [ ] Square Brackets | Pipe, vertical bar . Dot, point, period { } Curly braces < > Angle Brackets “ Double Quote, tick - Dash, hyphen, minus : Colon % Percent sign ( ) Parentheses ; Semi-colon + Plus = Equals, is ! Bang, not /, \ Slash, backslash
  • #16 begin with a $ Name that Language… Similar to @variables in SQL, apparently also Ruby, PHP. Arrays start at 0
  • #18 Mention Aliases
  • #21 Ask if anyone has unix scripting experience, and what they think of this. Can you imagine trying to do this in unix or vbscript?
  • #24 Get-Help about_flow_control Mention that collections start at 0
  • #25 Ask if anyone has unix scripting experience, and what they think of this. Can you imagine trying to do this in unix or vbscript?
  • #30 Making .jpgs for study cards Ripping cds into podcasts Ripping mp4s, fixing filenames
  • #44 Windows PowerShell in Action by Bruce Payette, V2 Since then Hicks and Jones have written some good ones
  • #47 http://technet.microsoft.com/en-us/scriptcenter/default
  • #48 PoshCode.Org TechNet Script Center http://www.microsoft.com/technet/scriptcenter/default.mspx MSDN SharePoint Developer Center http://msdn.microsoft.com/en-us/office/aa905503.aspx http://www.powergui.org/downloads.jspa http://thepowershellguy.com/blogs/posh/pages/powertab.aspx
  • #49  You owe it to yourself to try out PowerShell. It will make you a better developer!