Saturday, August 11, 2018

windows - Start elevated command prompt in %UserProfile% directory by default?


When I start an elevated Command Prompt in Windows 7 or Windows Vista, the initial directory is the %SystemRoot%\system32 directory.


This is the default Windows behavior, and is not a problem specific to my system, so I'm not expecting some sort of "bug fix" here, but rather a tweak to "fix" the default behavior.


To repro:



  • Click Start

  • Type cmd.exe

  • Hit Ctrl+Shift+Enter

  • When the elevation prompt appears, select Yes


Expected:



  • The initial directory is my user profile directory, e.g. C:\Users\merlyn


Actual:



  • The initial directory is the system32 directory, C:\Windows\system32


I'd like this be easy to work with. I'd like it to apply to all cmd.exe prompts/shortcuts, regardless of where they're started from. E.g. I'd like it to automatically apply to my Visual Studio Command Prompt (2010) without having to modify that specific shortcut.


I'd like this to be safe so that batch files aren't broken by my fixes.


Is there an easy and safe way to get any elevated command prompt to start in the %UserProfile% directory by default?


Answer



Opening an elevated command-prompt always starts in %systemroot%\System32, the rationale being that if you are doing something requiring elevated privileges, you are likely to be working on system files as opposed to just the user’s own files which you can do without elevated privileges.


You can work around it:



  1. Open a registry editor (e.g., regedit)

  2. Navigate to HKEY_CURRENT_USER\Software\Microsoft\Command Processor

  3. Edit the Autorun value to read cd /d %userprofile%


Now whenever you open a command-prompt, it will either start in or switch to, as the case may be, the user profile directory. Also, instead of HKCU, you can set the value in the HKLM branch to do it for all users on the system.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...