Category Archives: ExecutionPolicy

Set-ExecutionPolicy

NAME
    Set-ExecutionPolicy

SYNOPSIS
    Changes the user preference for the Windows PowerShell execution policy.

SYNTAX
    Set-ExecutionPolicy [-ExecutionPolicy] {Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined} [[-Scope] {Process | CurrentUser | LocalMachine | UserPolicy | MachinePolicy}] [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Set-ExecutionPolicy changes the user preference for the Windows PowerShell execution policy.

    To run this command on Windows Vista, Windows Server 2008, and later versions of Windows, you must start Windows PowerShell with the “Run as administrator” option, even if you are a member of the Administrators group on the computer.

    The execution policy is part of the security strategy of Windows PowerShell. It determines whether you can load configuration files (including your Windows PowerShell profile) and run scripts, and it determines which scripts, if any, must be digitally signed before they will run.

    For more information, see about_execution_policies.

PARAMETERS
    -ExecutionPolicy <ExecutionPolicy>
        Specifies a new execution policy for the shell. The parameter name (“Name”) is optional.

        Valid values are:

        — Restricted: Does not load configuration files or run scripts. “Restricted” is the default.

        — AllSigned: Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.

        — RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.

        — Unrestricted: Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.

        — Bypass: Nothing is blocked and there are no warnings or prompts.

        — Undefined: Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Force [<SwitchParameter>]
        Suppresses all prompts. By default, Set-ExecutionPolicy displays a warning whenever you change the execution policy.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Scope <ExecutionPolicyScope>
        Specifies the scope of the execution policy. The default is LocalMachine.

        Valid values are:

        — Process: The execution policy affects only the current Windows PowerShell process.
        — CurrentUser: The execution policy affects only the current user.
        — LocalMachine: The execution policy affects all users of the computer.

        To remove an execution policy from a particular scope, set the execution policy for that scope to Undefined.

        Required?                    false
        Position?                    2
        Default value                LocalMachine
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    Microsoft.PowerShell.ExecutionPolicy, System.String
        You can pipe an execution policy object or a string that contains the name of an execution policy to Set-ExecutionPolicy.

OUTPUTS
    None
        This cmdlet does not return any output.

NOTES

        When you use Set-ExecutionPolicy, the new user preference is written to the Registry and remains unchanged until you change it.

        However, if the “Turn on Script Execution” Group Policy is enabled for the computer or user, the user preference is written to the Registry, but it is not effective, and Windows PowerShell displays a message explaining the conflict. You cannot use Set-ExecutionPolicy to override a Group Policy, even if the user preference is more restrictive than the policy.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Set-ExecutionPolicy remotesigned

    Description
    ———–
    This command sets the user preference for the shell execution policy to RemoteSigned.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Set-ExecutionPolicy Restricted

    Set-ExecutionPolicy : Windows PowerShell updated your local preference successfully, but the setting is overridden by the group policy applied to your system. Due to the override, your shell will retain its current effective execution policy of “AllSigned”. Contact your group policy administrator for more information.
    At line:1 char:20
    + Set-ExecutionPolicy <<<< restricted

    Description
    ———–
    This command attempts to set the execution policy for the shell to “Restricted.” The “Restricted” setting is written to the Registry, but because it conflicts with a Group Policy, it is not effective, even though it is more restrictive than the policy.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Invoke-Command -computername Server01 -scriptblock {Get-ExecutionPolicy} | Set-ExecutionPolicy -Force

    Description
    ———–
    This command gets the execution policy from a remote computer and applies that execution policy to the local computer.

    The command uses the Invoke-Command cmdlet to send the command to the remote computer. Because you can pipe an ExecutionPolicy (Microsoft.PowerShell.ExecutionPolicy) object to Set-ExecutionPolicy, the Set-ExecutionPolicy command does not need an ExecutionPolicy parameter.

    The command does have a Force parameter, which suppresses the user prompt.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy AllSigned -Force

    C:\PS> Get-ExecutionPolicy -list

            Scope ExecutionPolicy
            —– —————
    MachinePolicy         Undefined
     UserPolicy         Undefined
         Process         Undefined
     CurrentUser         AllSigned
     LocalMachine     RemoteSigned

    C:\PS> Get-ExecutionPolicy
    AllSigned

    Description
    ———–
    This example shows how to set an execution policy for a particular scope.

    The first command uses the Set-ExecutionPolicy cmdlet to set an execution policy of AllSigned for the current user. It uses the Force parameter to suppress the user prompts.

    The second command uses the List parameter of Get-ExecutionPolicy to get the execution policies set in each scope. The results show that the execution policy that is set for the current user differs from the execution policy set for all users of the computer.

    The third command uses the Get-ExecutionPolicy cmdlet without parameters to get the effective execution policy for the current user on the local computer. The result confirms that the execution policy that is set for the current user takes precedence over the one set for all users.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined

    Description
    ———–
    This command uses an execution policy value of Undefined to effectively remove the execution policy that is set for the current user scope. As a result, the execution policy that is set in Group Policy or in the LocalMachine (all users) scope is effective.

    If you set the execution policy in all scopes to Undefined and the Group Policy is not set, the default execution policy, Restricted, is effective for all users of the computer.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113394
    Get-ExecutionPolicy
    Set-AuthenticodeSignature
    Get-AuthenticodeSignature
    about_execution_policies
    about_Signing

Get-ExecutionPolicy

NAME
    Get-ExecutionPolicy

SYNOPSIS
    Gets the execution policies for the current session.

SYNTAX
    Get-ExecutionPolicy [[-Scope] {Process | CurrentUser | LocalMachine | UserPolicy | MachinePolicy}] [-List] [<CommonParameters>]

DESCRIPTION
    The Get-ExecutionPolicy cmdlet gets the execution policies for the current session.

    The execution policy is determined by execution policies that you set by using Set-ExecutionPolicy and the Group Policy settings for the Windows PowerShell execution policy. The default value is “Restricted.”

    Without parameters, Get-ExecutionPolicy gets the execution policy that is effective in the session. You can use the List parameter to get all execution policies that affect the session or the Scope parameter to get the execution policy for a particular scope.

    For more information, see about_execution_policies.

PARAMETERS
    -List [<SwitchParameter>]
        Gets all execution policy values for the session listed in precedence order. By default, Get-ExecutionPolicy gets only the effective execution policy.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Scope <ExecutionPolicyScope>
        Gets the execution policy in the specified scope. By default, Get-ExecutionPolicy gets the effective execution policy for the current session.

        Valid values are:

        — MachinePolicy: The execution policy set by a Group Policy for all users of the computer.
        — UserPolicy: The execution policy set by a Group Policy for the current user of the computer.
        — Process: The execution policy that is set for the current Windows PowerShell process.
        — CurrentUser: The execution policy that is set for the current user.
        — LocalMachine: The execution policy that is set for all users of the computer.

        Required?                    false
        Position?                    1
        Default value                Effective execution policy
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    Microsoft.PowerShell.ExecutionPolicy
        Get-ExecutionPolicy returns an object for each execution policy that it gets.

NOTES

        You cannot use Get-ExecutionPolicy to get particular execution policies set for a particular scope or to get the execution policy set by a Group Policy. Get-ExecutionPolicy only gets the effective execution policy that results from applying all precedence rules.

        The execution policy is part of the security strategy of Windows PowerShell. It determines whether you can load configuration files (including your Windows PowerShell profile) and run scripts, and it determines which scripts, if any, must be digitally signed before they will run.

        The effective execution policy is determined by the policies that you set by using Set-ExecutionPolicy and the “Turn on Script Execution” group policies for computers and users. The precedence order is Computer Group Policy > User Group Policy > Process (session) execution policy > User execution policy > Computer execution policy.

        For more information about Windows PowerShell execution policy, including definitions of the Windows PowerShell policies, see about_execution_policies.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-ExecutionPolicy

    Restricted

    Description
    ———–
    This command gets the current execution policy for the shell.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Set-ExecutionPolicy RemoteSigned; Get-ExecutionPolicy

    RemoteSigned

    Description
    ———–
    These commands set a new user preference for the shell execution policy and then display the effective execution policy. The commands are separated by a semicolon (;). In this example, because there is no Group Policy setting, the user preference is the effective policy for the shell.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-ExecutionPolicy -List

            Scope ExecutionPolicy
            —– —————
    MachinePolicy Undefined
     UserPolicy Undefined
         Process Undefined
     CurrentUser AllSigned
     LocalMachine RemoteSigned

    C:\PS> Get-ExecutionPolicy
    AllSigned

    Description
    ———–
    These commands get all execution policies in the current session and the effective execution policy.

    The first command gets all execution policies that affect the current session. The policies are listed in precedence order.

    The second command gets only the effective execution policy, which is the one set in the CurrentUser scope.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113315
    Set-ExecutionPolicy
    Get-AuthenticodeSignature
    Set-AuthenticodeSignature
    about_execution_policies
    about_Signing