SlideShare a Scribd company logo
Practical PowerShell Programming
for
Professional People
Ben Ten
(@Ben0xA)
Converge Detroit 2014
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
About Me
Ben Ten (0xA)
@Ben0xA - twitter
Chicago - #burbsec
Vice President
Security Officer
Developer
PoshSec Framework Developer / Creator
Gamer
Geek
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Overview
● Languages and Development
● PowerShell Scripting
● Resources
● Q&A
● PSA: This is mostly live code scripting in
PowerShell. Please code along with me!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Overview
Feel free to interrupt and ask questions!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Before we begin, a bit of a primer!
● Styles of Coding
● Syntax
● Getting Help
● Starting Out
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Styles of Coding/Scripting/Development
● Novice
● Avid Scripter
● Full Time Developer
● Code Monkey
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Styles of Coding/Scripting/Development
● Novice
● Avid Scripter
● Full Time Developer
● Code Monkey
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Syntax
syn•tax (sĭn tăks ) – the rules that governˈ ˌ
how a script, or program, is developed in a
given language.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Syntax
White Space, parens (), commas, periods,
quotes (“ vs '), tabs, braces [], curly
brackets {}, colons :, semi-colons ;, all play
an integral part in the syntax of a
language!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
RTF Manual/Docs/Reference
Often times, the documentation will have
an answer for what you are trying to
accomplish. *NOT ALWAYS THOUGH*
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
Interactive Help
● ?
● F1
● Intellisense (Ctrl+Space)
● Get-Help
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
Search Engines FTW!
Google is not the end all in searches. For
Development I prefer DuckDuckGo!
https://duckduckgo.com
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell is a task automation and
configuration management framework
from Microsoft, consisting of a command-
line shell and associated scripting
language built on the .NET Framework.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell was designed by :
● Jeffrey Snover (@jsnover)
● Bruce Payette (@BrucePayette)
● James Truher
Initial release was November 14, 2006
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell is a part of the Windows
Management Framework. WMF 5.0 was
released on April 3, 2014.
For today's scripting we will be using WMF
3.0.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
You will need:
● Windows Management Framework 3.0
● Microsoft .NET Framework 4.5
● Text Editor (your choice)
● Sublime Text http://www.sublimetext.com/
● Komodo Edit http://komodoide.com/komodo-edit/
● PowerShell ISE (comes with WMF)
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Cmdlets, Functions, and Scripts Oh My!
From a functional standpoint, cmdlets,
functions, and scripts are practically the
same.
They are a way to call a specific block of
code.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Cmdlet:
Written in a compiled .NET language.
Easier to deploy.
Help files are easier to write.
Has support for parameter validation.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Function:
Written in a PowerShell language.
Has to be deployed with a library.
Help is written inside the function.
Parameter validation has to be done in the
function itself.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Script:
Written in a PowerShell language.
Is invoked by calling the .ps1 file.
Deployed by itself or in a manifest file.
Can contain functions.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Set-ExecutionPolicy
Before you can run your custom scripts
you have to set the ExecutionPolicy to
RemoteSigned.
In PowerShell type:
Set-ExecutionPolicy RemoteSigned
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
PowerShell
HelloWorld.ps1
Enough of the primer! Let's get coding!
This is where you code along with me if
you can!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Variable(s):
a symbolic name associated with a value
and whose associated value may be
changed.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Hard-Coded:
Typing the value directly into your script.
Our “Hello World” text was hard-coded.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
PowerShell Variables:
A PowerShell variable is defined with the
dollar sign $ followed by the name of the
variable.
For example: $message is a variable.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
PowerShell Variables:
Let's rewrite our HelloWorld.ps1 to use a
variable $message with our text “Hello
World”.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Quotes! Single vs Double
Double Quotes (“) will attempt to resolve
any variables before anything is printed to
the screen.
Single Quotes (') will print exactly what is
typed between the quotes.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Backtick `
The backtick, or grave accent, is a special
escape character. This means that you
want the next character to be printed and
not interpreted in anyway.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Getting Input
Write-Output is great. But how do you get
information from a user?
Read-Host
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Getting Input
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Getting Input
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition is:
a feature of a programming language
which perform a different set of
computations or actions depending on
whether a programmer-specified boolean
condition evaluates to true or false.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition is:
Is the stop light is green? Keep going.
Is the stop light is red? Stop.
Is the stop light is yellow? Floor it!!!!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition expressed:
● If - Beginning of the condition.
● Else - Evaluates only if preceding condition(s)
is(are) false.
● ElseIf – Evaluates if preceding condition(s)
is(are) false with a new condition.
● Switch – Multiple conditions for a single
variable or object.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-and = both conditions must be true.
-or = only one of the conditions must be
true.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-eq = Equals
-lt = Less Than
-gt = Greater Than
-ne = Not Equal
-ge = Great Than or Equal
-le = Less Than or Equal
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Operator Precedence:
When operators have equal precedence,
Windows PowerShell evaluates them from
left to right. The exceptions are the
assignment operators, the cast operators,
and the negation operators (!, -not, -bnot),
which are evaluated from right to left.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Operator Precedence:
You can use enclosures, such as
parentheses, to override the standard
precedence order and force Windows
PowerShell to evaluate the enclosed part
of an expression before an unenclosed
part.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Parameters
A Parameter is:
A variable that allows you to pass an
object to a Cmdlet, Function, or Script.
Get-ChildItem
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Parameters
Get-Help Get-ChildItem
Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]>
[-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]]
[<CommonParameters>
Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include
-LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]]
[<CommonParame
Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force]
[-UseTransaction] [<CommonParameters>]
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Objects vs Text
PowerShell is Object Based.
Even if you see text on the screen, that
text is actually a “String” object.
You can access the members of the object
using the . operator after the variable
name.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Objects vs Text
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Piping
Piping is:
a way of moving something, unchanged,
from one place to another.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Piping
Piping is represented by the | (pipe)
character.
A pipe takes the object from the left side
and passes it to the right side.
Note: When passing to another cmdlet, $_
is used to reference the passed object.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Piping
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Piping
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Loops
Loops:
A way to perform the same block of code
for a specific number of times, until a
specific condition is met, or while a
specific condition exists.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Loops
Loops:
● ForEach
● ForEach-Object
● For
● While
● Do While
● Do Until
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Comments
Comments are defined by the # symbol.
Block comments are enclosed with <# and
#>.
.SYNOPSIS
.DESCRIPTION
.PARAMETER
.EXAMPLE
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Comments
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Putting it all Together
The final program!
Requirements:
● Search all files.
● Find the ones that were modified in a
specific date range.
● Create a list of those files and display
them.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Pitfalls
Don't overuse the Pipe! Not everything has
to be done in a single line.
It's more important that you understand
the code before you try to condense it to a
single line.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Pitfalls
With Loops, start small then open the
valve all the way!
You can get more than you wanted, or get
stuck in an endless loop.
Especially true when doing File operations!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Resources
Freenode (irc.freenode.net)
#PowerShell, #pssec, #poshsec channels.
Learn Windows PowerShell in a Month of
Lunches ~ Don Jones
Carlos Perez – PowerShell Workshop at
DerbyCon.
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Contact - Q&A
Ben Ten (0xA)
@Ben0xA - twitter
http://ben0xa.com
https://poshsec.org
web@ben0xa.com
Ben0xA – LinkedIn, Github, keybase, etc.
irc.freenode.net
#burbsec, #poshsec, #pssec
QUESTIONS?!
Practical PowerShell Programming for Professional People
Converge Detroit - Ben Ten (@Ben0xA)
Thank You!

More Related Content

What's hot (20)

PDF
De-mystifying contributing to PostgreSQL
Lætitia Avrot
 
PDF
Modern Perl for the Unfrozen Paleolithic Perl Programmer
John Anderson
 
PPTX
Php test fest
Barry O Sullivan
 
PPTX
Zend con 2016 bdd with behat for beginners
Adam Englander
 
PDF
Composer - The missing package manager for PHP
Tareq Hasan
 
PDF
Zend expressive workshop
Adam Culp
 
PDF
Mixing Plone and Django for explosive results
Simone Deponti
 
PPTX
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
PDF
Theory and practice – migrating your legacy code into our modern test drive...
Lars Jankowfsky
 
PDF
Automate Yo' Self
John Anderson
 
PPTX
Netflix JavaScript Talks - Scaling A/B Testing on Netflix.com with Node.js
Chris Saint-Amant
 
PDF
mod_php vs FastCGI vs FPM vs CLI
Jacques Woodcock
 
PDF
HTTP demystified for web developers
Peter Hilton
 
PDF
Becoming A Php Ninja
Mohammad Emran Hasan
 
PPTX
Take control. write a plugin. part II
Baruch Sadogursky
 
PDF
PHP 4? OMG! A small vademecum for obsolete software migration.
Francesco Fullone
 
PDF
Measuring Code Quality in WTF/min.
David Gómez García
 
ODP
Zero to Zend Framework in 10 minutes
Jeremy Kendall
 
RTF
Hack language
Ravindra Bhadane
 
PPTX
C# 6
Pascal Laurin
 
De-mystifying contributing to PostgreSQL
Lætitia Avrot
 
Modern Perl for the Unfrozen Paleolithic Perl Programmer
John Anderson
 
Php test fest
Barry O Sullivan
 
Zend con 2016 bdd with behat for beginners
Adam Englander
 
Composer - The missing package manager for PHP
Tareq Hasan
 
Zend expressive workshop
Adam Culp
 
Mixing Plone and Django for explosive results
Simone Deponti
 
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
Theory and practice – migrating your legacy code into our modern test drive...
Lars Jankowfsky
 
Automate Yo' Self
John Anderson
 
Netflix JavaScript Talks - Scaling A/B Testing on Netflix.com with Node.js
Chris Saint-Amant
 
mod_php vs FastCGI vs FPM vs CLI
Jacques Woodcock
 
HTTP demystified for web developers
Peter Hilton
 
Becoming A Php Ninja
Mohammad Emran Hasan
 
Take control. write a plugin. part II
Baruch Sadogursky
 
PHP 4? OMG! A small vademecum for obsolete software migration.
Francesco Fullone
 
Measuring Code Quality in WTF/min.
David Gómez García
 
Zero to Zend Framework in 10 minutes
Jeremy Kendall
 
Hack language
Ravindra Bhadane
 

Viewers also liked (20)

PDF
Powershell training material
Dr. Awase Khirni Syed
 
PPTX
PowerShell 101 - What is it and Why should YOU Care!
Thomas Lee
 
ODP
An Introduction to Windows PowerShell
Dale Lane
 
PPTX
Use Powershell to make your life easy.
Aman Dhally
 
PPT
Windows Server 2008 (PowerShell Scripting Uygulamaları)
ÇözümPARK
 
PPTX
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Richard Calderon
 
PDF
Power on, Powershell
Roo7break
 
PPTX
PowerShell Plus v4.7 Overview
Richard Giles
 
PPT
Powershell Seminar @ ITWorx CuttingEdge Club
Essam Salah
 
PPTX
Office 365 & PowerShell - A match made in heaven
Sébastien Levert
 
PDF
PowerShell from *nix user perspective
Juraj Michálek
 
PPT
Managing Virtual Infrastructures With PowerShell
guesta849bc8b
 
PDF
PowerShell UIAtomation
Juraj Michálek
 
PPTX
PowerShell 101
Thomas Lee
 
PPTX
Incorporating PowerShell into your Arsenal with PS>Attack
jaredhaight
 
PDF
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Puppet
 
PPT
Introduction to PowerShell
Salaudeen Rajack
 
PPTX
Geek Sync | Using PowerShell with Python and SQL Server
IDERA Software
 
PPTX
Network Mapping with PowerShell
Costin-Alin Neacsu
 
PPTX
Workshop: PowerShell for Penetration Testers
Nikhil Mittal
 
Powershell training material
Dr. Awase Khirni Syed
 
PowerShell 101 - What is it and Why should YOU Care!
Thomas Lee
 
An Introduction to Windows PowerShell
Dale Lane
 
Use Powershell to make your life easy.
Aman Dhally
 
Windows Server 2008 (PowerShell Scripting Uygulamaları)
ÇözümPARK
 
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Richard Calderon
 
Power on, Powershell
Roo7break
 
PowerShell Plus v4.7 Overview
Richard Giles
 
Powershell Seminar @ ITWorx CuttingEdge Club
Essam Salah
 
Office 365 & PowerShell - A match made in heaven
Sébastien Levert
 
PowerShell from *nix user perspective
Juraj Michálek
 
Managing Virtual Infrastructures With PowerShell
guesta849bc8b
 
PowerShell UIAtomation
Juraj Michálek
 
PowerShell 101
Thomas Lee
 
Incorporating PowerShell into your Arsenal with PS>Attack
jaredhaight
 
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Puppet
 
Introduction to PowerShell
Salaudeen Rajack
 
Geek Sync | Using PowerShell with Python and SQL Server
IDERA Software
 
Network Mapping with PowerShell
Costin-Alin Neacsu
 
Workshop: PowerShell for Penetration Testers
Nikhil Mittal
 
Ad

Similar to Practical PowerShell Programming for Professional People (20)

PPTX
Power shell training
David Brabant
 
PPTX
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
PPTX
PowerShell-1
Saravanan G
 
PPTX
Introduction to PowerShell and getting started
Ravikanth Chaganti
 
PPTX
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
PDF
Sql Server & PowerShell
Aaron Shilo
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
PPTX
In just one hour i will make you a power shell ninja
Jason Brown
 
PPTX
Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)
Michael Blumenthal (Microsoft MVP)
 
PDF
Powershell notes
Carlos Amorim
 
PPTX
PowerShellForDBDevelopers
Bryan Cafferky
 
PDF
PowerShell in Depth An administrator s guide Don Jones
lekwalakha3p
 
PPTX
Introduction to windows power shell in sharepoint 2010
Binh Nguyen
 
PDF
Windows Powershell Step By Step 3rd Edition Wilson Ed
forsenqenan
 
PPTX
PowerShell 101
Thomas Lee
 
PPTX
Introduction to powershell
Salaudeen Rajack
 
PPTX
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PPTX
PowerShell for Developers
Damian Powell
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Power shell training
David Brabant
 
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
PowerShell-1
Saravanan G
 
Introduction to PowerShell and getting started
Ravikanth Chaganti
 
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
Sql Server & PowerShell
Aaron Shilo
 
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
In just one hour i will make you a power shell ninja
Jason Brown
 
Introduction to PowerShell (SharePoint Fest Chicago 2016 Workshop)
Michael Blumenthal (Microsoft MVP)
 
Powershell notes
Carlos Amorim
 
PowerShellForDBDevelopers
Bryan Cafferky
 
PowerShell in Depth An administrator s guide Don Jones
lekwalakha3p
 
Introduction to windows power shell in sharepoint 2010
Binh Nguyen
 
Windows Powershell Step By Step 3rd Edition Wilson Ed
forsenqenan
 
PowerShell 101
Thomas Lee
 
Introduction to powershell
Salaudeen Rajack
 
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PowerShell for Developers
Damian Powell
 
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Ad

Recently uploaded (20)

PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 

Practical PowerShell Programming for Professional People

  • 1. Practical PowerShell Programming for Professional People Ben Ten (@Ben0xA) Converge Detroit 2014
  • 2. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) About Me Ben Ten (0xA) @Ben0xA - twitter Chicago - #burbsec Vice President Security Officer Developer PoshSec Framework Developer / Creator Gamer Geek
  • 3. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Overview ● Languages and Development ● PowerShell Scripting ● Resources ● Q&A ● PSA: This is mostly live code scripting in PowerShell. Please code along with me!
  • 4. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Overview Feel free to interrupt and ask questions!
  • 5. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Before we begin, a bit of a primer! ● Styles of Coding ● Syntax ● Getting Help ● Starting Out
  • 6. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Styles of Coding/Scripting/Development ● Novice ● Avid Scripter ● Full Time Developer ● Code Monkey
  • 7. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Styles of Coding/Scripting/Development ● Novice ● Avid Scripter ● Full Time Developer ● Code Monkey
  • 8. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Syntax syn•tax (sĭn tăks ) – the rules that governˈ ˌ how a script, or program, is developed in a given language.
  • 9. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Syntax White Space, parens (), commas, periods, quotes (“ vs '), tabs, braces [], curly brackets {}, colons :, semi-colons ;, all play an integral part in the syntax of a language!
  • 10. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Getting Help! RTF Manual/Docs/Reference Often times, the documentation will have an answer for what you are trying to accomplish. *NOT ALWAYS THOUGH*
  • 11. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Getting Help! Interactive Help ● ? ● F1 ● Intellisense (Ctrl+Space) ● Get-Help
  • 12. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development Getting Help! Search Engines FTW! Google is not the end all in searches. For Development I prefer DuckDuckGo! https://duckduckgo.com
  • 13. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Languages and Development
  • 14. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Overview PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command- line shell and associated scripting language built on the .NET Framework.
  • 15. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Overview PowerShell was designed by : ● Jeffrey Snover (@jsnover) ● Bruce Payette (@BrucePayette) ● James Truher Initial release was November 14, 2006
  • 16. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Overview PowerShell is a part of the Windows Management Framework. WMF 5.0 was released on April 3, 2014. For today's scripting we will be using WMF 3.0.
  • 17. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell You will need: ● Windows Management Framework 3.0 ● Microsoft .NET Framework 4.5 ● Text Editor (your choice) ● Sublime Text http://www.sublimetext.com/ ● Komodo Edit http://komodoide.com/komodo-edit/ ● PowerShell ISE (comes with WMF)
  • 18. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell File Name Extensions .ps1 – Script Files .psm1 – Script Module Files .psd1 – Script Manifest Files .ps1xml – Formatting and Type Files .dll - Cmdlet and Provider Assemblies
  • 19. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell File Name Extensions .ps1 – Script Files .psm1 – Script Module Files .psd1 – Script Manifest Files .ps1xml – Formatting and Type Files .dll - Cmdlet and Provider Assemblies
  • 20. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Cmdlets, Functions, and Scripts Oh My! From a functional standpoint, cmdlets, functions, and scripts are practically the same. They are a way to call a specific block of code.
  • 21. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Cmdlet: Written in a compiled .NET language. Easier to deploy. Help files are easier to write. Has support for parameter validation.
  • 22. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Function: Written in a PowerShell language. Has to be deployed with a library. Help is written inside the function. Parameter validation has to be done in the function itself.
  • 23. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Script: Written in a PowerShell language. Is invoked by calling the .ps1 file. Deployed by itself or in a manifest file. Can contain functions.
  • 24. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell Set-ExecutionPolicy Before you can run your custom scripts you have to set the ExecutionPolicy to RemoteSigned. In PowerShell type: Set-ExecutionPolicy RemoteSigned
  • 25. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell
  • 26. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) PowerShell HelloWorld.ps1 Enough of the primer! Let's get coding! This is where you code along with me if you can!
  • 27. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 28. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 29. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 30. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 Variable(s): a symbolic name associated with a value and whose associated value may be changed.
  • 31. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 Hard-Coded: Typing the value directly into your script. Our “Hello World” text was hard-coded.
  • 32. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 PowerShell Variables: A PowerShell variable is defined with the dollar sign $ followed by the name of the variable. For example: $message is a variable.
  • 33. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 PowerShell Variables: Let's rewrite our HelloWorld.ps1 to use a variable $message with our text “Hello World”.
  • 34. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 35. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 36. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 37. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 38. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 Quotes! Single vs Double Double Quotes (“) will attempt to resolve any variables before anything is printed to the screen. Single Quotes (') will print exactly what is typed between the quotes.
  • 39. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 40. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 41. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 42. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 43. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1 Backtick ` The backtick, or grave accent, is a special escape character. This means that you want the next character to be printed and not interpreted in anyway.
  • 44. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 45. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 46. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 47. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) HelloWorld.ps1
  • 48. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Getting Input Write-Output is great. But how do you get information from a user? Read-Host
  • 49. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Getting Input
  • 50. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Getting Input
  • 51. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Condition is: a feature of a programming language which perform a different set of computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
  • 52. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Condition is: Is the stop light is green? Keep going. Is the stop light is red? Stop. Is the stop light is yellow? Floor it!!!!
  • 53. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Condition expressed: ● If - Beginning of the condition. ● Else - Evaluates only if preceding condition(s) is(are) false. ● ElseIf – Evaluates if preceding condition(s) is(are) false with a new condition. ● Switch – Multiple conditions for a single variable or object.
  • 54. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Conditional Operator: -and = both conditions must be true. -or = only one of the conditions must be true.
  • 55. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Conditional Operator: -eq = Equals -lt = Less Than -gt = Greater Than -ne = Not Equal -ge = Great Than or Equal -le = Less Than or Equal
  • 56. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic A Conditional Operator: -Like -NotLike -Match -NotMatch -Contains -NotContains -In -NotIn -Replace
  • 57. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 58. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 59. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic Operator Precedence: When operators have equal precedence, Windows PowerShell evaluates them from left to right. The exceptions are the assignment operators, the cast operators, and the negation operators (!, -not, -bnot), which are evaluated from right to left.
  • 60. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic Operator Precedence: You can use enclosures, such as parentheses, to override the standard precedence order and force Windows PowerShell to evaluate the enclosed part of an expression before an unenclosed part.
  • 61. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 62. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 63. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 64. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Conditional Logic
  • 65. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Parameters A Parameter is: A variable that allows you to pass an object to a Cmdlet, Function, or Script. Get-ChildItem
  • 66. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Parameters
  • 67. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Parameters Get-Help Get-ChildItem Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]> [-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]] [<CommonParameters> Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include -LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]] [<CommonParame Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force] [-UseTransaction] [<CommonParameters>]
  • 68. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Parameters
  • 69. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Parameters
  • 70. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Objects vs Text PowerShell is Object Based. Even if you see text on the screen, that text is actually a “String” object. You can access the members of the object using the . operator after the variable name.
  • 71. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Objects vs Text
  • 72. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Piping Piping is: a way of moving something, unchanged, from one place to another.
  • 73. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Piping Piping is represented by the | (pipe) character. A pipe takes the object from the left side and passes it to the right side. Note: When passing to another cmdlet, $_ is used to reference the passed object.
  • 74. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Piping
  • 75. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Piping
  • 76. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Loops Loops: A way to perform the same block of code for a specific number of times, until a specific condition is met, or while a specific condition exists.
  • 77. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Loops Loops: ● ForEach ● ForEach-Object ● For ● While ● Do While ● Do Until
  • 78. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Loops
  • 79. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Loops
  • 80. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Loops
  • 81. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Comments Comments are defined by the # symbol. Block comments are enclosed with <# and #>. .SYNOPSIS .DESCRIPTION .PARAMETER .EXAMPLE
  • 82. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Comments
  • 83. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Putting it all Together The final program! Requirements: ● Search all files. ● Find the ones that were modified in a specific date range. ● Create a list of those files and display them.
  • 84. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Pitfalls Don't overuse the Pipe! Not everything has to be done in a single line. It's more important that you understand the code before you try to condense it to a single line.
  • 85. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Pitfalls With Loops, start small then open the valve all the way! You can get more than you wanted, or get stuck in an endless loop. Especially true when doing File operations!
  • 86. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Resources Freenode (irc.freenode.net) #PowerShell, #pssec, #poshsec channels. Learn Windows PowerShell in a Month of Lunches ~ Don Jones Carlos Perez – PowerShell Workshop at DerbyCon.
  • 87. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Contact - Q&A Ben Ten (0xA) @Ben0xA - twitter http://ben0xa.com https://poshsec.org [email protected] Ben0xA – LinkedIn, Github, keybase, etc. irc.freenode.net #burbsec, #poshsec, #pssec QUESTIONS?!
  • 88. Practical PowerShell Programming for Professional People Converge Detroit - Ben Ten (@Ben0xA) Thank You!