You are on page 1of 29

Module 8

Advanced
Windows PowerShell
Tips and Tricks

Module Overview
Create and safely use a Windows PowerShell profile script
Repackage scripts as script modules for easier distribution

Add comment-based help to functions, scripts, and script

modules

Lesson 1: Using Profiles


Explain how profiles are executed and identify

predetermined profile filenames and locations

Explain the security concerns associated with profile

scripts

Create a profile script

Why Profiles?
Each time you start a new PowerShell session, you begin

with a fresh environment

Default configurations

Default aliases

Default everything!

Profiles in Windows PowerShell provide a mechanism to

customize the shell environment automatically with each


new session

Preload modules, custom aliases, connect PSDrives, anything


that is invoked via a valid PowerShell command

PowerShell searches for a predetermined file in a


predetermined location for this script

You can change this script to customize your working


environment

Discussion: Whats in Your Profile?


What kinds of things would you add to a Windows

PowerShell profile script?

Profiles Do Not Always Run


Profile scripts will not execute in certain circumstances:

When you connect to a remote instance of the shell, the profile


script in the remote instance will not run

When the shell is embedded inside another application, such


as the Exchange Server Manager application

When the shells execution policy is set to Restricted, or if the


execution policy is set to AllSigned and the profile script is not
signed

Rule of thumb: You should expect profile scripts to

execute only when you are using an interactive shell


window, via the console or Windows PowerShell ISE

Profile Locations
Profile scripts can be found

For the current user in $Home\[My ]Documents\profile.ps1

For all users in $PsHome\Profile.ps1

Host applications can also load their own profile scripts

Current user for the console in $Home\[My ]Documents\


WindowsPowerShell\profile.ps1

All users for the console in


$PsHome\Microsoft.PowerShell_Profile.ps1

Current user for the ISE in $Home\[My ]Documents\


indowsPowerShell\Microsoft.PowerShellISE_profile.ps1

All users for the ISE in


$PsHome\Microsoft.PowerShellISE_profile.ps1
Multiple profiles mean that each host will load up to four
different profiles. To reduce complexity, consider
standardizing on Current User profiles.

Profile Security
Caution: Your profile script could be used as a vector for

malware attack, because

It is a simple text file

It lives in your Documents folder

It executes automatically

The best protection against this potential form of attack is

to run anti-malware software on your computer

Also, do not store profile scripts on computers where you do


not normally use the interactive shell or run scripts

On computers where you do not routinely run scripts, leave


the shell execution policy at Restricted

Demonstration: Using Profiles


Learn how to use Windows PowerShell profile scripts

Lesson 2: Reusing Scripts and Functions


Dot source a script into the shell or another script
Package a script as a script module and import it into the

shell

Modify a script module to expose only selected functions

when the module is imported into the shell

Functions
A function is a way of writing your own command

In fact, some functions behave almost exactly like the cmdlets


youve already been using

The last module in this course teaches how to create your own
functions

But you may receive functions from colleagues, or you may


find functions on the Internet that you want to use

One way to re-use functions is to paste them into the

script file where you want to use them

However, this is not necessarily efficient. If you later need to


change that function, youll need to change it everywhere.

Also, pasting a function into a script doesnt necessarily make


that function available at the command line

This lesson will discuss a set of tactics you can use to


enhance the re-use of your scripts

Discussion: Script Reuse


What kinds of functions or scripts do you think you might

create that may be used in many different places?

Functions that check for computer connectivity

Functions that retrieve specific pieces of information from a


computer

Functions that import or export data, such as from or to a


database

Quick Concept: Scope


Recall: PowerShell works with a complex hierarchy of

scopes

A scope acts as a sort of box, inside which various shell

elements execute

A child scope can READ elements from its parents

A parent scope can never read or write anything in its child


scopes

You can WRITE only to the current scope

If you try to change something in a parent scope, the result


will be a new item of that same name being created in the
current scope

Dot Sourcing
Dot sourcing is a way of eliminating the box around a

script. It is a means of including one script within another.

When you dot source a script, you are running it in your


current scope rather than creating a new scope

Anything defined by the dot sourced script remains after the


script finishes running, including variables, PSDrives, aliases,
functions, and so forth
. ./library.ps1

Dot sourcing enables you to keep your reusable functions

in one or more library scripts

When you need to use a library function, dot source the library
script that contains the function

This process defines the function in the current scope, making


it available in that scope for future use

Demonstration: Using Dot Sourcing


Learn how to dot source a script

Script Modules
Dot sourcing is effective, but it is also confusing
Script modules offer a more formal and structured way to

re-use scripts and functions

Easier to distribute to others

Offer the ability to include multiple files, such as views

A script module is a normal shell script with a .psm1

extension that is stored in a predefined location

Predefined locations can be viewed with the command:

Dir env:\psmodulepath

Modules are created and uniquely named in the script file.


They are then subsequently loaded for use with the command:
Import-Module ModuleName

Demonstration: Using Script Modules


Learn how to use Windows PowerShell script modules

More Script Module Features


Script modules can also include internal, or private

functions

These functions are for use within the script itself, but are not
exposed to users who import the module into their shell.

The Export-ModuleMember cmdlet

Controls which items are exposed when the module is


exported. Anything not explicitly exported will not be exposed,
but will be available internally.

Script modules can also contain manifests

Manifests list your script module and its supporting files, such
as formatting views, that must be loaded with your module.

The New-ModuleManifest cmdlet

Creates new manifest files, which are then customized to list


the necessary files.

Demonstration: Export-ModuleMember
Learn how to create private functions within a script

module

Discussion: Sharing Modules


Script modules offer an easy way to share functions and

scripts with colleagues and peers

What sort of capabilities might you package into a script


module for easier sharing?

Lesson 3: Writing Comment-Based Help


Add comment-based help to a function
Access a functions comment-based help from within the

shell

Help
Youve already experienced the robust built-in help

features that PowerShell and its cmdlets provide

Command syntax

Usage examples

Shell capabilities

You can build help into your own scripts and functions

that looks exactly like the shells built-in help

Custom help does not require XML formatting

Building help into your scripts makes their reusable code

more easily-accessible to others

This is particularly the case for advanced functions because


these functions behave much like real cmdlets

Comment-Based Help
Comment-based help can be added to scripts or functions

It uses a set of specially formatted comments to define


various sections of the help.

It must appear as the first set of lines in the script or script


module or immediately after the function keyword.

Four commonly used help sections

SYNOPSIS Provides a brief statement of what the script or


function does.

DESCRIPTION Provides a more detailed statement of what


the script or function does.

PARAMETER followed by a parameter name, describes the


purpose of a single parameter. If you have more than one
parameter, include a separate PARAMETER section for each
one.

EXAMPLE provides a usage example. You can have multiple


EXAMPLE sections.

Demonstration: Adding Comment-Based Help


Learn how to add comment-based help to a script or

function

Discussion: Using Comment-Based Help


Would comment-based help replace or supplement the

kind of in-line comments that you usually expect to find in


a script?

Many organizations develop standards for in-line

comments inside scripts or other programs

Would your organization develop similar standards for using


comment-based help?

Lab: Advanced PowerShell Tips and Tricks


Exercise 1: Writing a Profile Script
Exercise 2: Creating a Script Module

Exercise 3: Adding Help Information to a Function

Logon information
Virtual machine

LON-SVR1

Logon user name

Contoso\Administrator

Password

Pa$$w0rd

Estimated time: 30 minutes

Lab Scenario
You are a system administrator and have been developing

lots of scripts and functions for several months.

You would like to easily pass this knowledge to your co-

workers and also provide them with good documentation


on how to use what you have developed.

You have also developed a few custom commands that

you want to ensure are always available in your interactive


shell. To accomplish this, you will add those commands to
a profile script.

Lab Review
Whats the difference between adding comment-based

help to a function versus adding lots of regular comments


in the script itself?

Why is it important to refrain from using customized

aliases in a module that is shared with others?

Module Review and Takeaways


Can comment-based help include references to other help

topics, or even to online content?

Would you be more likely to use an all-hosts profile or a

current-host profile?

You might also like