Start Powershell with specified execution policy without changing default

Sponsored links

How to start powershell

I often starts powershell.exe from an explorer instead of command prompt because it looks better than command prompt and powershell command is available. As you may know applications can be started from the directory where you currently open in windows explorer by specifying the command into directory path display area.

Sponsored links

Long directory path problem

But when you input “powershell” into the directory path display area it is started with the default execution policy setting which doesn’t allow you to load powershell script. This got me irritated because I wanted to load powershell script which includes a function to change a way to show directory path. Isn’t it hard to see if the directory path and your command are long?

Ignore the content of the image below.

I wanted to make the directory path short!! When powershell starts it tries to load “DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1” but powershell cannot load this file because of execution policy!!

I don’t want to change the default setting because of security but I want to change it when I want to start powershell from windows explorer…

Solution

Add a command file

I created a directory “C:commands” to place files which configure for the applications that I want to start. I placed “ps.bat” file there and the content is following.
start powershell -ExecutionPolicy RemoteSigned

Add the Path to the file

The directory path is not included into a environment path. Therefore, windows cannot find the file even if you input the command name into directory path display area.“C:commands” must be added to environment variable. The step is following.

  1. Open Control panel > System > Advanced system settings
  2. Open Environment setting
  3. Click Path variable and click edit
  4. Add “C:commands
  5. Click OK

Try to start

Open a directory with windows explorer and input “ps” and tap Enter key. The error message has gone because powershell is executed by ps.bat which set execution policy to RemoteSigned.

How to show current directory name

If you also want to show current directory name short directory path you need to define a function in
“<users>DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1”
I defined following code. This shows the current directory name.

function prompt() {
  (Split-Path (Get-Location) -Leaf) + " > "
}

Conclusion

To make your work smooth you need to do a little work but it’s worth doing. In this post, I wrote about powershell but it can be applied to other applications too if they can somehow be configured, via configuration file or command options for example.

Comments

Copied title and URL