Running Scripts

This topic explains how to enable scripting in Windows PowerShell, which script extensions are used in Windows PowerShell, and how to run a script.

Enabling Windows PowerShell Scripts to Run

When you start Windows PowerShell on a computer, the default security policy does not allow you to run scripts. The Windows PowerShell security policy for scripting is called an execution policy. The execution policy lets you determine whether scripts can run in your environment and whether they must include a digital signature. None of the execution policies in Windows PowerShell allow you to run a script by double-clicking its icon because that is a high-risk method of running a script.

The following execution policies govern scripting in Windows PowerShell:

Restricted. Permits interactive commands only (no scripts). This is the default.

AllSigned. Permits scripts, but requires a digital signature from a trusted publisher for all scripts and configuration files, including scripts that you write on the local computer.

RemoteSigned. Permits scripts, but requires a digital signature from a trusted publisher for all scripts and configuration files that are downloaded from the Internet, including e-mail. A digital signature is not required for scripts that you create on the local computer.

Unrestricted. Permits scripts, including unsigned scripts.

Because the default Windows PowerShell execution policy is Restricted, you cannot run Windows PowerShell scripts until you change to a less restrictive execution policy. The following table lists Windows PowerShell Help topics that explain what you need to know about Windows PowerShell execution policies and how to change your policy so that you can run scripts.

To get information about Windows PowerShell execution policies

Type at the command prompt

Description

Get-Help about_Signing

Displays information about Windows PowerShell execution policies and the levels of security that the execution policies provide.

Get-Help Get-ExecutionPolicy

Displays information that explains how to determine your current scripting security policy.

Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1 is the Windows PowerShell console file for Microsoft System Center Operations Manager.

For more information about Windows PowerShell console files, type Get-Help Export-Console at the command prompt.

Windows PowerShell format and type definitions

.ps1xml

A type of script file that provides a mechanism for extending the Microsoft .NET Framework type system. These script files are in the Windows PowerShell home directory (<C>:\WINDOWS\SysWOW64\Windowspowershell\v1.0), For more information, type Get-Help about_Types at the command prompt.

Running a Windows PowerShell Script

When you run a Windows PowerShell script, you must always indicate the full path with the name of the script even if you are working in the directory in which the script is located. You can use the following methods to run a Windows PowerShell script:

Use the dot and the backslash (.\) to indicate the local directory. For example:

.\ <ScriptName>.ps1

Specify the full path of the script. For example:

C:\Scripts\<ScriptName>.ps1

Specify the path of the script, but omit the extension. For example:

C:\Scripts\<ScriptName>

Use the Invoke-Expression cmdlet to run a script. For example:

Invoke-Expression C:\Scripts\<ScriptName>.ps1

Use double quotation marks for any paths that include spaces. For example:

Invoke-Expression "C:\My Scripts\<ScriptName>.ps1"

Use the ampersand to run a script. For example:

& C:\Scripts\<ScriptName>.ps1

Important

You can create a Profile.ps1 script that is configured to enable you to run scripts without typing the full path. However, if you do not do this, you must indicate the full path with the name of the script even if you are working in the directory in which the script is located.