Pages

Wednesday, June 10, 2009

Changing Prompt And Execution Policy in PowerShell

In this article I am going to show you, how to change the prompt of your PowerShell and few lines about Execution Policy in PowerShell.

When you start the PowerShell I am getting following prompt
PS H:\>




I want to change this PS H:\> to PowerShell VJ:\>.

In order to change this first you need to understand Execution Policy. Execute the
get-ExecutionPolicy command to find out the current execution policy.

PS H:\> get-ExecutionPolicy
Restricted
PS H:\>


Execution Policy is not thing but set of rules that governs how PowerShell execute scripts on your machine.

There are 4 different execution policies in PowerShell.

Restricted:
If the Current Execution Policy is set to Restricted then You can't exeucte any scripts (.ps1) or configuration (.ps1xml) files.

AllSigned:
If the Current Execution Policy is set to AllSigned then Scripts (.ps1) and Configuration (.ps1xml) files must be digitally signed.

Remote Signed: Ps1 and .Ps1xml files from the internet must be digitally signed.
If the Current Execution Policy is set to RemoteSigned then Any Scripts (.ps1) and Configuration (.ps1xml) files which are downloaded from the internet must be digitally signed.

Unrestricted: No digital signatures are required and all scripts can be executed.
If the Current Execution Policy is set to Unrestricted then All scripts and configuration files can be executed.

There are two different ways in which you can change the execution policy.
The first method, using Set-ExecutionPolicy command and second method is using Windows Registry. I do not recommend you to modify the execution policy using Windows Registry.

Using Set-Execution Policy
PS H:\> set-executionpolicy Unrestricted
PS H:\>

What this does is, it will create an entry in the registry in the following location.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

"ExecutionPolicy"="UnRestricted "

Using Registry
Goto the following location
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

Add new string Value If it doesn’t exists.

Name = ExecutionPolicy
Value = UnRestricted

Now lets see, how to change the prompt.
Create “WindowsPowerShell” Folder in "My Documents" Folder and create profile.ps1 file.

Put the following lines of code in that

function prompt
{
"PowerShell DotNetVJ:\> "
}


And start the PowerShell to see new the Prompt.

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PowerShell DotNetVJ:\>

That’s it.

2 comments:

Anonymous said...

Changing the execution policy to 'Unrestricted' also removes any hope of protection you have from scripting viruses or malware. The about_signing topic goes into much more detail, but the RemoteSigned execution policy is probably the best trade-off of security and usability.

Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation

Vijaya Kadiyala said...

Hi Lee,
I am really happy to see comments from you on my PowerShell articles.
I would like to study and get more information about this Execution Policy but couldn't find :(.
It would be great if you could show me the direction on this.
I have couple of other questions on powershell. Can i contact you on your email!!
Thanks -- Vijaya Kadiyala