<<

Basic PowerShell Scripting: Level 1

What You Will Learn...... 1

What is PowerShell ...... 2 Leveraging ....... 2

PowerShell ISE ...... 3 PowerShell ISE Console ...... 3 PowerShell ISE Toolbar ...... 4 PowerShell ISE Windows ...... 5

Objects ...... 6 Members, Properties and Methods ...... 6 Using the or Method of an Object ...... 7

PowerShell Commands ...... 8 Breaking Out of a ...... 8 PowerShell Command Types and Aliases ...... 8 Commands that Provide ...... 10 Line Continuation ...... 12

Pipelines ...... 13

Execution Policy ...... 15 Execution Policy ...... 15 Local Policy Setting and Setting ...... 16 Determine the Execution Policy ...... 16 Setting the Execution Policy ...... 16 To Display the Current User Policy Setting ...... 17 Remove the Execution Policy ...... 17 Setting the Scope of the Execution Policy ...... 17

PowerShell Scripts ...... 18 What is a PowerShell Script ...... 18 Running Scripts from the Current Directory ...... 18 Running Scripts from a Different Directory ...... 19 Specifying Parameters (Arguments) when Running Scripts ...... 19

PowerShell Variables ...... 20 Variable Declaration and Naming ...... 20 The $ Method of Declaring Variables and Viewing Variable Values ...... 20 Declaring and Initializing Multiple Variables ...... 21 Assignment Operators ...... 22 Comparison Operators ...... 24 Get-Variable Cmdlet ...... 26 Set-Variable and New-Variable Cmdlets ...... 26

Basic PowerShell Scripting: Level 1 www.cetc.umsl.edu Remove-Variable Cmdlet ...... 32 Data Types ...... 32 PowerShell Variables and the Read-Host Cmdlet ...... 35 The .NET Framework Graphical of Getting User Input ...... 35 Environment Variables ...... 36

Exercise 1 ...... 38

Literals ...... 39 String Literals ...... 39 Special Characters (Escape Characters) ...... 42 Here-String String Literals ...... 43 SubString Method ...... 46 The Length Property ...... 47 Trimming Strings ...... 48 Numbers and Numeric Literals ...... 49

Exercise 2 ...... 51

Conditional If Statement ...... 52

Exercise 3 ...... 56

Arrays or Lists...... 57

Exercise 4 ...... 64

Hash Tables: Associative Arrays ...... 65

Appendix A: Operators ...... 67

Appendix B: Aliases for Class Cmdlets...... 72

Appendix C: Solutions to Exercises ...... 73

Basic PowerShell Scripting: Level 1 www.cetc.umsl.edu