Subscribe to Windows IT Pro
May 20, 2009 12:00 AM

Save Your PowerShell Code in Profile and Script Files

The final lesson in the PowerShell 201 series explores how to make PowerShell code last for more than just one session
Windows IT Pro
InstantDoc ID #101718
Rating: (2)
Executive Summary:
In this final lesson of the PowerShell 201 series, you'll learn how to create profile files and script files in Windows PowerShell and how to add PowerShell statements to them. You'll also learn how to access the code in profile and script files from the console and how to use input parameters in script files. With this foundation, you'll be able to create PowerShell code that meets your specific needs and that will ultimately streamline the steps necessary to perform a wide variety of administrative tasks.

In Windows PowerShell, any functions, variables, or other language elements that you defined during your session will be lost when you exit the PowerShell console. However, that doesn't have to be the case. You can use profile files and script files to save your code. Profile files make the code available whenever you start PowerShell, whereas script files make the code available on demand.

I'll walk you through how to create both types of files and how to add PowerShell statements to them. I'll also show you how to access the code in profile and script files from the console and how to use input parameters in script files. With this foundation, you'll be able to create code that meets your specific needs and that will ultimately streamline the steps necessary to perform a wide variety of administrative tasks.

Creating a Profile File

A profile file is a text file with a specific name and path, both of which are predefined by PowerShell. When you start PowerShell, it reads existing profile files and loads their code into memory. Any code you define in a profile file is available to you during your sessions. For example, if a profile file includes a function, you can call that function from the PowerShell console without having to enter that function's code.

You must create your own profile file. The easiest way to do this is to take advantage of PowerShell's built-in $profile variable. This variable stores the fully qualified pathname of the current user's individual profile file. The variable is predefined with this information whether or not a profile file has been created. You can view the $profile value by running the command

$profile

The exact path stored in $profile will vary depending on the user and the OS. For example, in Windows Vista, the path will look similar to C:\Users\user1\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. In Windows XP, the path will look similar to C:\Documents and Settings\user1\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

To create a profile file with the $profile variable, you need to use the New-Item cmdlet and specify $profile as the -path parameter's value, as in

New-Item -path $profile `
  -itemType file -force

This command specifies file as the -itemType value and includes the -force parameter, which overrides certain existing restrictions when creating the file. For example, if the file already exists, the -force parameter will overwrite the existing file.

After you create the file, you can open it by calling Notepad from within PowerShell with the code

Notepad $profile

When you run this command, Notepad opens and displays a blank file. You can then add PowerShell statements to the file. Note that if you run this command and the file doesn't exist, you'll receive an error saying that system can't find the path specified.

Adding Content to a Profile File

To add content to a profile file, you simply enter the statements as you would enter them in the PowerShell console. For example, you can define a variable such as

# Retrieve day and date.
$today = Get-Date -displayHint date

The $today variable retrieves the current day and date. When adding code to a profile file, it's always good practice to include comments that specify the purpose of the code, like the comment shown here (the line preceded by the number sign). PowerShell ignores any lines preceded by a number sign.

You can add as many statements as necessary to your profile. For example, you can include the function

# Retrieve 10 most recent events.
function events ($log="system")
{
  Get-EventLog $log -newest 10
}

The events function retrieves the most recent 10 events in the specified log. If you don't specify a log as an input parameter when you call the function, it retrieves events from the System log. (For information about user-defined functions, see "Create Your Own PowerShell Functions." After you finish entering statements in the profile file, save it and close Notepad. Figure 1 shows a sample profile file.

Figure 1: Adding the $today variable and the events function to the profile file

To use the profile file, you must restart your PowerShell session. However, before you do so, you should verify PowerShell's current execution policy. The execution policy determines whether a script file can run—and a profile file is a type of script file. Script files and profile files are text files with a .ps1 extension. By default, PowerShell sets its execution policy to Restricted, which means that script files will not run and profile files will not load.

To verify the current execution policy, run the command

Get-ExecutionPolicy

If PowerShell returns a setting of Restricted, as Figure 2 shows, you can use the Set-ExecutionPolicy cmdlet to change the execution policy. Before you decide which execution policy to use, see PowerShell's About Signing and Execution Policies Help file to view details about the four different policies. Once you decide on the policy, you can run a command such as

Set-ExecutionPolicy RemoteSigned

This command sets the execution policy to RemoteSigned. After you set the policy, you can run the Get-ExecutionPolicy cmdlet again to verify that the change was made, as shown in Figure 2.

Figure 2: Setting the execution policy

Related Content:

ARTICLE TOOLS

Comments
  • Jeff
    3 years ago
    Jun 09, 2009

    I have been enjoying Robert's column for several months now. He has provided an intuitive, easy to follow description of PowerShell essentials. Any administrator who isn't comfortable with the command line now has a great reason to pick it up.

    Kudos to Robert. I look forward to his upcoming articles...

    Jeff D.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.