38

How do I change my default home directory to "C:\Users\khornsby" for every time I open powershell?

I am running Windows 7. Below is info about the state of my environment.

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

PS P:\> cd ~
PS P:\> echo $HOME
P:\
PS P:\> HOME="C:\Users\khornsby"
The term 'HOME=C:\Users\khornsby' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:25
+ HOME="C:\Users\khornsby" 

PS P:\> Set-Variable HOME "C:\Users\khornsby"
Set-Variable : Cannot overwrite variable HOME because it is read-only or
constant.
At line:1 char:13
+ Set-Variable 

PS P:\> dir env:home*

Name                           Value
----                           -----
HOMEPATH                       \
HOMEDRIVE                      P:
HOMESHARE                      \\fileserv\khornsby$


PS P:\>
2
  • 5
    How did your format your post? I like the way what you typed is highlighted.
    – Jay Bazuzi
    Commented Feb 8, 2010 at 16:53
  • 7
    I used the <kbd> tag.
    – kzh
    Commented Feb 21, 2010 at 12:55

5 Answers 5

33

The variable is read only by default, but can be removed with the -Force switch to Remove-Variable. To make your change persistent across sessions, you can make a profile file which is much like a .bashrc (For bash on Linux) except for Powershell.

In your Documents directory (normally C:\Users\YOUR_USERNAME_HERE\documents) for your user account, create a WindowsPowerShell folder (named exactly like that) if one does not already exist. Inside the folder, create a text file called profile.ps1 (ensure it's not profile.ps1.txt).

Inside the file, place anything you want executed when you open Powershell.

example:

Write-Host "Hi John, welcome back!"
Remove-Variable -Force HOME
Set-Variable HOME "C:\Users\khornsby"

result:

alt text

4
  • 1
    You can also have the script run on startup from a shortcut, pointing to: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy bypass -File C:\foo\profile.ps1
    – paradroid
    Commented Apr 15, 2011 at 11:28
  • 1
    I tried this, and it does successfully change the value of $HOME. But "cd ~" still stubbornly switches to the original location.
    – Weeble
    Commented Dec 19, 2013 at 14:57
  • 2
    @Weeble, see my answer for overriding the ~ shortcut.
    – ulty4life
    Commented Jan 3, 2014 at 9:59
  • This does not work when opening a terminal in VS Code.
    – Jan M.
    Commented Mar 14, 2023 at 10:23
14

To address @Weeble 's concern about the ~ shortcut:

$HOMEDRIVE = "C:\"
$HOMEPATH = "Users\" + $env:username

# Set and force overwrite of the $HOME variable
Set-Variable HOME "$HOMEDRIVE$HOMEPATH" -Force

# Set the "~" shortcut value for the FileSystem provider
(get-psprovider 'FileSystem').Home = $HOMEDRIVE + $HOMEPATH

See here for the distinction between ~ and $HOME

1
  • 1
    it's worth adding cd $HOME at the end, so the shell will start in that location and will be all set for use :) Commented Feb 12, 2015 at 13:04
7

To change from within Windows, try the following:

  • Pin PowerShell to the taskbar.

  • Right click the PowerShell icon on the taskbar.

  • Right click 'Windows PowerShell' and select 'Properties'.

  • Within the 'Properties' window, go to the 'Shortcut' tab and change the 'Start in:' field to your desired starting directory. (Example: C:\Users\username\Desktop).

  • Click 'OK'.

  • Launch PowerShell from the taskbar.

    taskbar

    Properties window

2
  • not works when select 'run as administrator'
    – gdbdable
    Commented Apr 17, 2019 at 8:58
  • 1
    this was the only thing that worked for me
    – Clay Smith
    Commented Oct 11, 2021 at 16:02
6

Even easier ... open up advanced system settings ...

 C:\> systempropertiesadvanced

Add a new system variable named HOME with the path to your profile

enter image description here

Restart explorer or log out and back in ...

PS C:\> $Env:home 
--- 
3
  • 4
    This doesn't do what the question asks. This sets the environment variable $env:HOME, not the Powershell variable $HOME. The Powershell variable $HOME appears to be derived from the environment variables $env:HOMEDRIVE and $env:HOMEPATH at the time the Powershell process starts. It is $HOME that determines the behaviour of "cd ~".
    – Weeble
    Commented Dec 19, 2013 at 14:50
  • 2
    Sorry, that last statement isn't quite true. $HOME and the behaviour of "cd ~" both appear to match the values of the HOMEDRIVE and HOMEPATH environment variables when the Powershell process starts. Subsequently changing any of these variables/environment variables appears to have no effect on the directory chosen by "cd ~".
    – Weeble
    Commented Dec 19, 2013 at 15:02
  • Works fine for me.... I don't know what you're doing differently. Commented Dec 23, 2013 at 1:20
1

You can use help about_profiles to see more details about this. Do not forget to sign your script.

2
  • 3
    help from what application?
    – kzh
    Commented Apr 15, 2011 at 11:20
  • @kzh: PowerShell
    – paradroid
    Commented Apr 15, 2011 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .