Why cd .. when you can just backup?

Lately I have been on a PowerShell kick writing several utility functions. You can find my previous posts here:

In this post I am going to add one more that allows you to go up several levels in your path without having to repeatedly type .. over and over again.  The new function is called Backup-Location with an alias of bu.

buCmdLet

First, we need to create the function. To do that I started Visual Studio code. I also installed the PowerShell extension for Code.

With Code ready you need to create a new file and changed the language mode from Plain Text to PowerShell. If you already read my previous posts you can open the file we created in those posts. Copy and paste the code below into the file.

##############################################################################
#.SYNOPSIS
# Sets the location n number of levels up.
#
#.DESCRIPTION
# You no longer have to cd ..\..\..\..\ to move back four levels. You can now
# just type bu 4
#
#.PARAMETER Levels
# Number of levels to move back.
#
#.EXAMPLE
# PS C:\Users\dlbm3\source\pullrequests\somePR\vsteam> bu 4
# PS C:\Users\dlbm3>
##############################################################################
function Backup-Location {
   [CmdletBinding()]
   param(
      [Parameter(Mandatory = $true)]
      [int] $Levels
   )

   $sb = New-Object System.Text.StringBuilder

   for($i = 1; $i -le $Levels; $i++) {
      $sb.Append("../") | Out-Null
   }

   Set-Location -Path $($sb.ToString())
 }

 Set-Alias bu Backup-Location

I got fancy this time and even added some help.

If you are continuing from my previous posts and used the same utils.ps1 file you are done.

If you are starting with this post, save the file so you we can dot source the file in our PowerShell profile. I named my file utils.ps1 and saved it in C:\Users\me\Documents\WindowsPowerShell\Scripts.

Now we must update our profile to dot source the file we just created. Start PowerShell and type the following:

code $Profile

This will start Code with your current profile script loaded or create a new file for you if you do not already have one.

Add to the bottom of the file

. C:\Users\me\Documents\WindowsPowerShell\Scripts\utils.ps1

Now just save and close the file. Now every new PowerShell session you start you will be able to use bu to navigate back several levels at a time.

Comments (6) -

  • Glego

    3/5/2018 7:30:31 AM | Reply

    Hi Donovan,

    Thanks for the script. I love the way you describe your powershell Synopsis. But why is the command not coherent with the description? It would make way more sense to use set-locationlevel or something similar.

    Cheers
    Glenn

  • Ron ben

    3/5/2018 5:42:09 PM | Reply

    You can use CD ~ to move back to your home directory or just type the literal path c:\users..
    I guess if u find this command useful you are navigate quite a lot..
    You could also use * for building the string easier
    $sb="../"*$Levels
    Nice coding ,good job

  • Zachary Loeber

    3/6/2018 3:41:26 PM | Reply

    Clever and useful. I'm going to add this to my helpers plugin for OhMyPsh. Check it out if you get a chance, good way to make your profile a bit more modular.

    Cheers,

  • Anon

    3/6/2018 3:51:24 PM | Reply

    You could replace all of that with this:

    function Backup-Location ($Levels) {
        $Path = "../" * $Levels
        Set-Location -LiteralPath $Path
    }

    • Donovan

      3/26/2018 7:29:19 PM | Reply

      Nice!  very cleaver.

  • Taleeb

    3/16/2018 2:38:26 AM | Reply

    I wish this if it was a standard command under Bash and PowerShell, in fact I did port you idea and created a script for Bash: midiroot.com/.../

Add comment

Loading