Setting $HOME in PowerShell

548

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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 
--- 

Solution 5

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

Share:
548

Related videos on Youtube

mugetsu
Author by

mugetsu

Updated on September 17, 2022

Comments

  • mugetsu
    mugetsu over 1 year

    here's my code:

    I = imread(fullfile(vl_root,'data','cup1.jpg')) ;
    %returns a picture of a cup
    image(I)
    colormap gray;
    %returns a white blank picture
    image(rgb2gray(I))
    

    what is strange is, the following code works for me:

        I = getsnapshot(vid);
     %returns a picture of a snapshot
        image(I)
        colormap gray;
        %returns a black and white  picture
        image(rgb2gray(I))
    

    from this I can only assume that there is some kind of difference between the two image types, but I can't seem to pinpoint why. They are all

    any ideas?

    • Jay Bazuzi
      Jay Bazuzi about 14 years
      How did your format your post? I like the way what you typed is highlighted.
    • Matt Roberts
      Matt Roberts about 14 years
      I used the <kbd> tag.
  • Matt Roberts
    Matt Roberts about 13 years
    help from what application?
  • Naidim
    Naidim about 13 years
    @kzh: PowerShell
  • Naidim
    Naidim about 13 years
    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
  • Weeble
    Weeble over 10 years
    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
    Weeble over 10 years
    I tried this, and it does successfully change the value of $HOME. But "cd ~" still stubbornly switches to the original location.
  • Weeble
    Weeble over 10 years
    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 ~".
  • ulty4life
    ulty4life over 10 years
    @Weeble, see my answer for overriding the ~ shortcut.
  • Eliran Malka
    Eliran Malka about 9 years
    it's worth adding cd $HOME at the end, so the shell will start in that location and will be all set for use :)
  • gdbdable
    gdbdable about 5 years
    not works when select 'run as administrator'
  • Clay Smith
    Clay Smith over 2 years
    this was the only thing that worked for me