Page 1
Varun Varghese +91 9986599326 [email protected] 1
What’s PowerShell• Scripting Environment• Scripting Language• Windows equivalent of UNIX Shell Script (but even more powerful)
Page 2
Varun Varghese +91 9986599326 [email protected] 2
Interactive Shell
Page 3
Varun Varghese +91 9986599326 [email protected] 3
History
Page 4
Varun Varghese +91 9986599326 [email protected] 4
Versions
Page 5
Varun Varghese +91 9986599326 [email protected] 5
.Net Framework
Page 6
Varun Varghese +91 9986599326 [email protected] 6
Installing the latest versionhttps://www.microsoft.com/en-us/download/details.aspx?id=46889
Page 7
Varun Varghese +91 9986599326 [email protected] 7
ISE – The PowerShell IDE
Page 8
Varun Varghese +91 9986599326 [email protected] 8
Case InsensitiveGet-serviceget-serviceGet-Service
Page 9
Varun Varghese +91 9986599326 [email protected] 9
Installing/Accessing PowerShell
Page 10
Varun Varghese +91 9986599326 [email protected] 10
Updating the help in PowerShell
Page 11
Varun Varghese +91 9986599326 [email protected] 11
Get help in PowerShellGet-Help
Page 12
Varun Varghese +91 9986599326 [email protected] 12
Execution Policy
Page 13
Varun Varghese +91 9986599326 [email protected] 13
Get & Set CmdletsGet-<Command>Set-<Command>
Page 14
Varun Varghese +91 9986599326 [email protected] 14
Comments
Page 15
Varun Varghese +91 9986599326 [email protected] 15
Writing to Console
Page 16
Varun Varghese +91 9986599326 [email protected] 16
Clear Screen
Page 17
Varun Varghese +91 9986599326 [email protected] 17
Aliases
Page 18
Varun Varghese +91 9986599326 [email protected] 18
Variables
Page 19
Varun Varghese +91 9986599326 [email protected] 19
Data types
Page 20
Varun Varghese +91 9986599326 [email protected] 20
Pipelining
.Net objects passed down the pipeline
Page 21
Varun Varghese +91 9986599326 [email protected] 21
Listing directory Contents
Page 22
Varun Varghese +91 9986599326 [email protected] 22
Recursive listing
Page 23
Varun Varghese +91 9986599326 [email protected] 23
ErrorAction SilentlyContinue
Page 24
Varun Varghese +91 9986599326 [email protected] 24
Looping using Foreach-Object
Page 25
Varun Varghese +91 9986599326 [email protected] 25
Filtering using Where-Object
Page 26
Varun Varghese +91 9986599326 [email protected] 26
Sorting using Sort-Object
Page 27
Varun Varghese +91 9986599326 [email protected] 27
PowerShell v/s UNIX Shell
Page 28
Varun Varghese +91 9986599326 [email protected] 28
Assignment Operators=+=-=*=/=%=++--
Page 29
Varun Varghese +91 9986599326 [email protected] 29
Comparison Operators-eq-ne-gt-ge-lt-le-replace-contains-notcontains-in-notin-match-notmatch-like-notlike
-is-isnot-as
Page 30
Varun Varghese +91 9986599326 [email protected] 30
Comparison Operators: -eq, -ne
Page 31
Varun Varghese +91 9986599326 [email protected] 31
Comparison Operators: -gt, -ge
Page 32
Varun Varghese +91 9986599326 [email protected] 32
Comparison Operators: -lt, -le
Page 33
Varun Varghese +91 9986599326 [email protected] 33
Comparison Operators: -replace
Page 34
Varun Varghese +91 9986599326 [email protected] 34
Comparison Operators: -contains, -notcontains
Page 35
Varun Varghese +91 9986599326 [email protected] 35
Comparison Operators: -in, -notin
Page 36
Varun Varghese +91 9986599326 [email protected] 36
Comparison Operators: -match, -notmatch
Page 37
Varun Varghese +91 9986599326 [email protected] 37
Comparison Operators: -like, -notlike
Page 38
Varun Varghese +91 9986599326 [email protected] 38
Comparison Operators: -is, -isnot
Page 39
Varun Varghese +91 9986599326 [email protected] 39
Comparison Operators: -as
Page 40
Varun Varghese +91 9986599326 [email protected] 40
Logical Operators
Page 41
Varun Varghese +91 9986599326 [email protected] 41
Split, Join, foreach
Page 42
Varun Varghese +91 9986599326 [email protected] 42
Casting
Page 43
Varun Varghese +91 9986599326 [email protected] 43
Redirection Operators
Page 44
Varun Varghese +91 9986599326 [email protected] 44
Handling arrays
Page 45
Varun Varghese +91 9986599326 [email protected] 45
Dictionary/hash tables
Page 46
Varun Varghese +91 9986599326 [email protected] 46
Object Properties
Page 47
Varun Varghese +91 9986599326 [email protected] 47
Object Methods
Page 48
Varun Varghese +91 9986599326 [email protected] 48
String manipulation
Page 49
Varun Varghese +91 9986599326 [email protected] 49
Sorting Objects
Page 50
Varun Varghese +91 9986599326 [email protected] 50
PowerShell automatic variables
Page 51
Varun Varghese +91 9986599326 [email protected] 51
Searching filesystem using PS
Page 52
Varun Varghese +91 9986599326 [email protected] 52
Getting System processes
Page 53
Varun Varghese +91 9986599326 [email protected] 53
Getting list of services
Page 54
Varun Varghese +91 9986599326 [email protected] 54
Filtering objectshttps://technet.microsoft.com/en-us/magazine/2007.04.powershell.aspx
http://windowsitpro.com/powershell/powershell-basics-filtering-objects
http://www.howtogeek.com/138324/geek-school-learning-formatting-filtering-and-comparing-in-powershell/
http://searchwindowsserver.techtarget.com/feature/Filtering-output-from-Windows-PowerShell
Page 55
Varun Varghese +91 9986599326 [email protected] 55
Copying files/directories
Page 56
Varun Varghese +91 9986599326 [email protected] 56
Reading from file
Page 57
Varun Varghese +91 9986599326 [email protected] 57
Writing to file
Page 58
Varun Varghese +91 9986599326 [email protected] 58
Appending to a file
Page 59
Varun Varghese +91 9986599326 [email protected] 59
Deleting file/directory
Page 60
Varun Varghese +91 9986599326 [email protected] 60
Renaming a file/directory
Page 61
Varun Varghese +91 9986599326 [email protected] 61
Reading from Excel
Page 62
Varun Varghese +91 9986599326 [email protected] 62
Reading from CSV
Page 63
Varun Varghese +91 9986599326 [email protected] 63
Writing to CSV
Page 64
Varun Varghese +91 9986599326 [email protected] 64
Writing to Excel
Page 65
Varun Varghese +91 9986599326 [email protected] 65
Export-CSV
Page 66
Varun Varghese +91 9986599326 [email protected] 66
Reading large files
Page 67
Varun Varghese +91 9986599326 [email protected] 67
Exporting as CSV
Page 68
Varun Varghese +91 9986599326 [email protected] 68
Importing CSV
Page 69
Varun Varghese +91 9986599326 [email protected] 69
Appending to file
Page 70
Varun Varghese +91 9986599326 [email protected] 70
User defined Functions
http://windowsitpro.com/windows/create-your-own-powershell-functions
Page 71
Varun Varghese +91 9986599326 [email protected] 71
Param in functions
Page 72
Varun Varghese +91 9986599326 [email protected] 72
Passing parameters to functions
Page 73
Varun Varghese +91 9986599326 [email protected] 73
Parameters from Pipeline
Page 74
Varun Varghese +91 9986599326 [email protected] 74
Error Handling
Page 75
Varun Varghese +91 9986599326 [email protected] 75
Logging Error
Page 76
Varun Varghese +91 9986599326 [email protected] 76
Exception Handlinghttp://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
http://blogs.msdn.com/b/kebab/archive/2013/06/09/an-introduction-to-error-handling-in-powershell.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2014/07/09/handling-errors-the-powershell-way.aspx
Page 77
Varun Varghese +91 9986599326 [email protected] 77
Output formattinghttps://technet.microsoft.com/en-us/library/dd347677.aspx
https://technet.microsoft.com/en-us/magazine/2008.06.windowspowershell.aspx
http://www.computerperformance.co.uk/powershell/powershell_format_table.htm
http://www.computerperformance.co.uk/powershell/powershell_-f_format.htm
http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/23/format-powershell-output-with-an-easy-to-use-table.aspx
http://windowsitpro.com/powershell/powershell-basics-formatting
Page 78
Varun Varghese +91 9986599326 [email protected] 78
Advanced formatting
Page 79
Varun Varghese +91 9986599326 [email protected] 79
Redirecting formatted output
Page 80
Varun Varghese +91 9986599326 [email protected] 80
Modules
Page 81
Varun Varghese +91 9986599326 [email protected] 81
Navigating the Registry
Page 82
Varun Varghese +91 9986599326 [email protected] 82
Regex
Page 83
Varun Varghese +91 9986599326 [email protected] 83
Starting a new Job
Page 84
Varun Varghese +91 9986599326 [email protected] 84
WMI• Windows Management Interface• Created by Microsoft• Is Microsoft’s implementation of CIM• CIM is the super class for WMI. Eq Win32_DiskDrive is derived from CIM_DiskDrive• Doesn’t work with the routers, switches or other network devices• Doesn’t support discoverabilty
Page 85
Varun Varghese +91 9986599326 [email protected] 85
WMIhttp://blogs.technet.com/b/heyscriptingguy/archive/2009/08/05/hey-scripting-guy-can-windows-powershell-call-wmi-methods.aspx
http://www.computerperformance.co.uk/powershell/powershell_wmi.htm
http://www.darkoperator.com/blog/2013/2/6/introduction-to-wmi-basics-with-powershell-part-2-exploring.html
http://www.lazywinadmin.com/2015/03/standard-and-advanced-powershell.html
WMI Administrative Toolshttp://www.microsoft.com/en-us/download/details.aspx?id=24045
Windows COM (Component Object Model)
Page 86
Varun Varghese +91 9986599326 [email protected] 86
WMI ExplorerDownload WMI Explorer from https://wmie.codeplex.com/
Page 87
Varun Varghese +91 9986599326 [email protected] 87
WQL (WMI Query Language)
Page 88
Varun Varghese +91 9986599326 [email protected] 88
CIM• Common Information Model• Set up by DTMF (Desktop Management Task Force)• http://www.dmtf.org/standards/cim• An open model compared to WMI• Common model for managing systems, switches, routers and other n/w
devices• Enables extension of the API by vendors• The goal is to make PowerShell the platform to manage both windows & non-
windows • Faster than WMI
“CIM provides a common definition of management information for systems, networks, applications and services, and allows for vendor extensions. CIM’s common definitions enable vendors to exchange semantically rich management information between systems throughout the network.”
Page 89
Varun Varghese +91 9986599326 [email protected] 89
PS Provider• Its an interface• It makes any storage look like a disk drive
• Could be filesystem, registry• Providers are used to create drives
Page 90
Varun Varghese +91 9986599326 [email protected] 90
https://technet.microsoft.com/en-us/library/ee126186(v=vs.85).aspx
https://technet.microsoft.com/en-us/library/dd315335.aspx
https://technet.microsoft.com/en-us/library/ee176857.aspx
PS Provider
Page 91
Varun Varghese +91 9986599326 [email protected] 91
PS DriveA PowerShell Drive is some sort of a storage adapted to look like a disk drive
Page 92
Varun Varghese +91 9986599326 [email protected] 92
PS Drive
Page 93
Varun Varghese +91 9986599326 [email protected] 93
New-PSDrive
Page 94
Varun Varghese +91 9986599326 [email protected] 94
Administration Automation
Page 95
Varun Varghese +91 9986599326 [email protected] 95
Connecting to SQL Server
Page 96
Varun Varghese +91 9986599326 [email protected] 96
Creating Database, Tables in SQL
Page 97
Varun Varghese +91 9986599326 [email protected] 97
Importing data into SQL Server
Page 98
Varun Varghese +91 9986599326 [email protected] 98
Connecting to Remote machines
Page 99
Varun Varghese +91 9986599326 [email protected] 99
Importing libraries
Page 100
Varun Varghese +91 9986599326 [email protected] 100
OOP in PowerShellhttps://psclass.codeplex.com/