Windows Vista Forums

Set-Location: Changing to user's home directory

  1. #1


    Gantenbein Guest

    Set-Location: Changing to user's home directory

    Would it be possible that a simple 'Set-Location' or 'cd' with no arguments
    changes to $home?



      My System SpecsSystem Spec

  2. #2


    Alex K. Angelopoulos [MVP] Guest

    Re: Set-Location: Changing to user's home directory

    What's the intent behind this? To me that seems a little awkward, but I may
    be over-influenced by prior expectations.

    You actually can get the same effect by using ~:
    Set-Location ~
    sl ~
    dir ~
    "Gantenbein" <nospam> wrote in message
    news:%23ueu8wDoGHA.3424@TK2MSFTNGP02.phx.gbl...
    > Would it be possible that a simple 'Set-Location' or 'cd' with no
    > arguments changes to $home?
    >




      My System SpecsSystem Spec

  3. #3


    Gantenbein Guest

    Re: Set-Location: Changing to user's home directory


    "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
    news:ufUb%236DoGHA.1204@TK2MSFTNGP04.phx.gbl...

    > What's the intent behind this? To me that seems a little awkward, but I
    > may be over-influenced by prior expectations.


    I'm used to that from Unix shells like bash.

    > You actually can get the same effect by using ~:
    > Set-Location ~
    > sl ~
    > dir ~



    Ok, maybe that's short enough.



      My System SpecsSystem Spec

  4. #4


    Alex K. Angelopoulos [MVP] Guest

    Re: Set-Location: Changing to user's home directory

    I see - I just tried that in Cygwin and of course it works.

    You should probably file this as a suggestion on the Connect site; it's
    worth doing since it clearly has precedent. If you post back with the URL
    for the suggestion once you make it, people can vote for it.

    https://connect.microsoft.com

    "Gantenbein" <nospam> wrote in message
    news:uItx9$DoGHA.780@TK2MSFTNGP04.phx.gbl...
    >
    > "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
    > news:ufUb%236DoGHA.1204@TK2MSFTNGP04.phx.gbl...
    >
    >> What's the intent behind this? To me that seems a little awkward, but I
    >> may be over-influenced by prior expectations.

    >
    > I'm used to that from Unix shells like bash.
    >
    >> You actually can get the same effect by using ~:
    >> Set-Location ~
    >> sl ~
    >> dir ~

    >
    >
    > Ok, maybe that's short enough.
    >




      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: Set-Location: Changing to user's home directory

    "Gantenbein" <nospam> wrote in message news:%23ueu8wDoGHA.3424@TK2MSFTNGP02.phx.gbl...
    > Would it be possible that a simple 'Set-Location' or 'cd' with no arguments
    > changes to $home?


    For what it's worth, this is the CD function that I use. It supports paths like "...." and is also stack based and accepts "cd -" and "cd +" to move up and down through the stack. If you execute "cd", it will display the current stack of directories:

    4# cd

    # Directory Stack:
    --- ----------------
    0 C:\
    1 C:\Documents and Settings\Keith
    2 C:\Temp
    3 C:\WINDOWS
    -> 4 C:\WINDOWS\Assembly\GAC

    You can then jump to a particular dir by number e.g. "cd -1" to cd to the directory in stack entry #1.

    --
    Keith

    function GetAdminStatus {
    # Does the current user had admin privileges
    $adminSid = [Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid
    $groups = $([Security.Principal.WindowsIdentity]::GetCurrent()).Groups
    foreach ($sid in $groups) {
    $txSid = $sid.Translate([Security.Principal.SecurityIdentifier])
    if ($txSid.IsWellKnown($adminSid)) {
    return $true
    }
    }
    return $false
    }

    # ---------------------------------------------------------------------
    # Custom CD function
    # ---------------------------------------------------------------------
    [bool]$global:__isAdmin = GetAdminStatus
    $global:__cd_backstack > $null
    $global:__cd_forestack > $null
    $global:__profileLoadCount += 1
    if ($global:__profileLoadCount -eq 1) {
    $global:__cd_windowTitle = "PoSH"
    }
    remove-item alias:cd -force -ea:silentlycontinue

    function cd ([string]$path) {
    # If more than one arg, let set-location handle this error
    if ($args.Length -gt 0) {
    write-error "Takes only one argument."
    return
    }

    # Handle the rest of the commands plus a path arg
    if ($path -eq "?") {
    ""
    "Usage: cd path | command"
    "Commands:"
    " <no arg> : Display the directory stack"
    " ~ : Change directory to your `$Home directory"
    " - : Change directory to the previous directory in the stack"
    " + : Change directory to the next directory in the stack"
    " -number : Change directory to the specified directory in the stack"
    " ..[.]* : Change directory to a parent directory of the current directory"
    " ? : Display this usage information"
    ""
    return
    }

    # If not created, initialize ArrayList to track directories
    if ($global:__cd_backstack -eq $null) {
    $global:__cd_backstack = new-object "System.Collections.ArrayList"
    $global:__cd_forestack = new-object "System.Collections.ArrayList"
    }

    $curPath = $(get-location)

    # Dump the directory stack
    if (!$path) {
    # Command to dump the back & fore stacks
    ""
    " # Directory Stack:"
    " --- ----------------"
    if ($global:__cd_backstack.Count -ge 0) {
    for ($i = 0; $i -lt $global:__cd_backstack.Count; $i++) {
    " {0,3} {1}" -f $i, $global:__cd_backstack[$i]
    }
    }

    write-host ("-> {0,3} {1}" -f $i++, $curPath)

    if ($global:__cd_forestack.Count -ge 0) {
    $ndx = $i
    for ($i = 0; $i -lt $global:__cd_forestack.Count; $i++) {
    " {0,3} {1}" -f ($ndx+$i), $global:__cd_forestack[$i]
    }
    }
    ""
    return
    }

    # Expand ..[.]+ out to ..\..[\..]+
    if ($path -like "*...*") {
    $regex = new-object System.Text.RegularExpressions.regex("\.\.\.")
    while ($regex.ismatch($path)) {
    $path = $regex.Replace($path, "..\..")
    }
    }

    if ($path -eq "-") {
    if ($global:__cd_backstack.Count -eq 0) {
    write-host "`nThe backward stack is empty.`n"
    }
    else {
    $lastNdx = $global:__cd_backstack.Count - 1
    $prevPath = $global:__cd_backstack[$lastNdx]
    set-location $prevPath
    [void]$global:__cd_forestack.Insert(0, $curPath)
    $global:__cd_backstack.RemoveAt($lastNdx)
    }
    }
    elseif ($path -eq "+") {
    if ($global:__cd_forestack.Count -eq 0) {
    write-host "`nThe foreward stack is empty.`n"
    }
    else {
    $nextPath = $global:__cd_forestack[0]
    set-location $nextPath
    [void]$global:__cd_backstack.Add($curPath)
    $global:__cd_forestack.RemoveAt(0)
    }
    }
    elseif ($path -like "-[0-9]*")
    {
    [int]$num = $path.replace("-","")
    $backstackSize = $global:__cd_backstack.Count
    $forestackSize = $global:__cd_forestack.Count
    if ($num -eq $backstackSize) {
    write-host "`nDude, you're already there!`n"
    }
    elseif ($num -lt $backstackSize) {
    $selectedPath = $global:__cd_backstack[$num]
    set-location $selectedPath
    [void]$global:__cd_forestack.Insert(0, $curPath)
    $global:__cd_backstack.RemoveAt($num)

    [int]$ndx = $num
    [int]$count = $global:__cd_backstack.Count - $ndx
    if ($count -gt 0) {
    $itemsToMove = $global:__cd_backstack.GetRange($ndx, $count)
    $global:__cd_forestack.InsertRange(0, $itemsToMove)
    $global:__cd_backstack.RemoveRange($ndx, $count)
    }
    }
    elseif ($num -gt $backstackSize -and $num -lt ($backstackSize + 1 + $forestackSize)) {
    [int]$ndx = $num - ($backstackSize + 1)
    $selectedPath = $global:__cd_forestack[$ndx]
    set-location $selectedPath
    [void]$global:__cd_backstack.Add($curPath)
    $global:__cd_forestack.RemoveAt($ndx)

    [int]$count = $ndx
    if ($count -gt 0) {
    $itemsToMove = $global:__cd_forestack.GetRange(0, $count)
    $global:__cd_backstack.InsertRange(($global:__cd_backstack.Count), $itemsToMove)
    $global:__cd_forestack.RemoveRange(0, $count)
    }
    }
    else {
    write-host "`n$num is out of range.`n"
    }
    }
    else
    {
    set-location $path -ea:stop
    $global:__cd_forestack.Clear();
    [void]$global:__cd_backstack.Add($curPath)
    }

    $adminStatus = ""
    if ($global:__isAdmin) { $adminStatus = " #" }

    $host.ui.rawui.windowTitle = $global:__cd_windowTitle + $adminStatus + " - " + $(get-location)
    }


      My System SpecsSystem Spec

  6. #6


    Bruce Payette [MSFT] Guest

    Re: Set-Location: Changing to user's home directory

    You can write your own cd function btw. Here's an example that is more UNIX
    like. It does
    cd # cd to home directory
    and
    cd - # cd to previous directory

    You can add this function to $PROFILE

    -bruce

    # remove any existing cd alias...
    rm -force alias:/cd 2> $null
    function cd {
    if ($args.Length) { $path = $args[0] } else { $path = '~' }

    # .'ing shortcuts
    switch ($path)
    {
    "..." { $path = "..\.." }
    "...." { $path = "..\..\.." }
    "-" {
    if (test-path variable:globalldpath)
    {
    write-host "cd'ing back to $global:OLDPATH"
    $temp = pwd
    set-location $global:OLDPATH
    $global:OLDPATH=$temp
    }
    else
    {
    write-host '$OLDPATH not set'
    }
    }
    default {
    $temp = pwd
    set-location $path
    if ($?) {
    $global:OLDPATH = $temp
    }
    else
    {
    write-host "cd failed!"
    }
    }
    }
    }


    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.



    "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
    news:ucNagfEoGHA.1592@TK2MSFTNGP04.phx.gbl...
    >I see - I just tried that in Cygwin and of course it works.
    >
    > You should probably file this as a suggestion on the Connect site; it's
    > worth doing since it clearly has precedent. If you post back with the URL
    > for the suggestion once you make it, people can vote for it.
    >
    > https://connect.microsoft.com
    >
    > "Gantenbein" <nospam> wrote in message
    > news:uItx9$DoGHA.780@TK2MSFTNGP04.phx.gbl...
    >>
    >> "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
    >> news:ufUb%236DoGHA.1204@TK2MSFTNGP04.phx.gbl...
    >>
    >>> What's the intent behind this? To me that seems a little awkward, but I
    >>> may be over-influenced by prior expectations.

    >>
    >> I'm used to that from Unix shells like bash.
    >>
    >>> You actually can get the same effect by using ~:
    >>> Set-Location ~
    >>> sl ~
    >>> dir ~

    >>
    >>
    >> Ok, maybe that's short enough.
    >>

    >
    >




      My System SpecsSystem Spec


Set-Location: Changing to user's home directory
Similar Threads
Thread Forum
User Home Directory PowerShell
changing user folders location in Vista Vista General
Changing the location of the user account Vista General
Re: Find AD user with specific home directory PowerShell
changing user directory to an other partition. Vista file management