PowerShell Basics
Chapter 5 of 18
Chapter 5: Turn Commands Into Scripts You Can Reuse
This chapter is where you stop retyping the same commands and start saving your work. A script is simply a file that holds commands you can run again, and that one change makes your work steadier and easier to share. You will also learn about execution policy, which is the setting that controls which script files are allowed to run. It is there to stop accidents, not to scare you, and you will learn a sensible setting for learning. By the end of this chapter, you will be able to write a short script, run it from its file, and explain why the type of a value changes what PowerShell does with it.
2 lessons 3 learning objectives 1 related command
Topics at a glance: Scripts and security: execution policy, Your first script and data types.
Where this chapter fits
This chapter comes after Chapter 4: Everyday Commands That Save Hours in the journey, so ideas from that chapter will feel familiar here.
After this chapter comes Chapter 6: Toolboxes You Can Load When You Need Them.
Learning objectives
By the end of this chapter, you will be able to:
- Explain what a script is and why execution policy exists
- Write and run a first script safely
- Use the common data types correctly
Lessons in this chapter
Work through the lessons in order. Each lesson page has explanations, examples, common mistakes, and a knowledge check.
Lesson 1: Scripts and security: execution policy
Execution policy is a safety setting. It controls whether script files are allowed to run, and it helps stop accidental or malicious scripts, but it is not a security boundary on its own.
3 things to learn in this lesson
Lesson 2: Your first script and data types
A first script is short: a comment, a variable, and an output line. Data types decide what you can do with a value, so learn the common ones early.
3 things to learn in this lesson
Related command references
These command pages are linked to lessons in this chapter. Each command page has its syntax, parameters, and examples.
Set-ExecutionPolicy
Sets the execution policy for scripts.
From the lesson: Scripts and security: execution policy
Tip: read the lesson first, then use the command page as a quick reference.