up vote 5 down vote favorite
3
share [g+] share [fb]

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:\>
link|improve this question

How did your format your post? I like the way what you typed is highlighted. – Jay Bazuzi Feb 8 '10 at 16:53
I used the <kbd> tag. – kzh Feb 21 '10 at 12:55
feedback

2 Answers

up vote 8 down vote accepted

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

link|improve this answer
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 Apr 15 '11 at 11:28
feedback

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

link|improve this answer
1  
help from what application? – kzh Apr 15 '11 at 11:20
@kzh: PowerShell – paradroid Apr 15 '11 at 11:23
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.