Lecture 10 – Functions Continued
Page 1 of 33
Lecture 10 Functions Continued and User
Last week we learned about function syntax, writing
help files, error checking, and passing named and
unnamed parameters and setting default values. This
week we will learn about scopes, the param keyword
and setting attributes for functions, such as making
parameters mandatory. We will also learn about how
to use the new AD cmdlets to create user and manage user accounts.
A “scope” is a security container which protects objects from being modified, intentionally or
unintentionally by a running script or function. Scripts and functions run in the local scope environment
can only change objects in the current scope, such as: Variables, Aliases, Functions and PowerShell
Drives (PSDrives). New scopes are created whenever you run a script or function, or when you create a
new session or instance of PowerShell. Scopes created by running scripts and functions have a
“parent/child” relationship with the scope from which they were created. There are a few scopes that
have particularly special meanings, and can be accessed by name:
The Global scope is the scope that is created when PowerShell starts. It includes the variables,
aliases, functions, and PSDrives that are built-in to PowerShell as well as any which are made by
your PowerShell profile.
The Local scope refers to whatever the current scope is. When you start PowerShell it will refer
to the Global scope, within a script it will be the Script scope, etc.
The Script scope is created when a script is run. The only commands that operate within this
scope are those that are in the script.
Private scopes can be defined within the current scope, to prevent commands in other scopes
Figure 1: Diagram of Scopes
© Seneca College, ICT, 2013