How do I make PowerShell run as administrator start in my home directory?

6

0

Whenever I run PowerShell as administrator, it starts in C:\WINDOWS\system32\WindowsPowerShell\v1.0. How do I make it start in my user's home directory instead? My $HOME has the expected value, i.e., C:\Users\<account>. Whenever I run PowerShell without administrative privileges it starts up in $HOME.

aknuds1

Posted 2011-11-14T14:27:10.103

Reputation: 8 668

Do you only want this to take effect when you open the PS CLI, or system-wide (ie: when scripts are run, etc.)? – Ƭᴇcʜιᴇ007 – 2011-11-14T14:55:26.390

@techie007 When I open the CLI. Hadn't actually considered the script case. – aknuds1 – 2011-11-14T15:06:59.197

Answers

8

If you always use a shortcut you can just add the arguments:

-NoExit -Command "cd ~"

If you want this to always execute you can make a profile, to do this create the file (and missing folders on the path):

%userprofile%\Documents\WindowsPowerShell\profile.ps1

And place the cd command (cd ~) inside it.

To allow the scripts execution on startup you need to change the execution policy to be less restrictive or bypass it.

To bypass you can pass an argument when starting powershell:

-ExecutionPolicy Bypass

To change the policy run powershell as admin and execute:

Set-Executionpolicy RemoteSigned

Do this at your own risk of course. If you did you will always end up in your home folder on startup.

H.B.

Posted 2011-11-14T14:27:10.103

Reputation: 521

1i liked the -noexit -command for my shortcut over putting a CD in my profile script... vscode runs my profile script but was moving to my home directory rather than the folder of the project i was in – Jeff Martin – 2018-08-16T20:06:42.583

Do you know why PowerShell defaults to another directory (than $HOME) when run with administrative privileges? Also, can I prevent "cd ~" from echoing the directory? – aknuds1 – 2011-11-15T13:25:03.413

@aknuds1: I do not know why it starts there, possibly because the system files is where the admin most likely wants to do something. What do you mean by "echoing the directory", cd should not create any output. – H.B. – 2011-11-15T16:15:49.083

Actually, for me, cd echoes the directory. I've tested by putting echo statements before and after the cd command in profile.ps1, and have that way verified this behaviour. – aknuds1 – 2011-11-15T19:49:24.700

Interesting, wonder why it's different... – H.B. – 2011-11-15T21:05:39.497