Shorten your PowerShell directory path

When working in a PowerShell session I often CD deep into the directory structure. This can make it difficult to construct complicated commands because they are forced to wrap across lines.


What I would like is to be able to address the current folder but as a drive letter instead.  Using the New-PSDrive cmdlet you can do just that. There are two problems with this.

  1. I never remember it is even there
  2. I can’t remember the syntax to use it

So I figured I would just wrap it in a function that is easy to remember and use.  I can then load my function using my profile so it is available in all my PowerShell sessions. In this post I will explain how to do this.

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 post on How to create and navigate a directory with a single command you can open the file we created in that post. Copy and paste the code below into the file.

 function set-as {
      [Parameter(Mandatory = $true)]

   New-PSDrive -PSProvider FileSystem -Name $Name -Root . -Scope Global | Out-Null
   Set-Location -LiteralPath "$($name):"

If you are continuing from my previous post 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 set-as to create and navigate into a folder.


Comments (1) -

  • Ivo Looser

    3/2/2018 7:51:27 AM | Reply

    Great idea to shorten the path using the FileSystem provider. Thanks a lot.
    That would have saved me a lot of trouble during the time I was teaching PowerShell.

Pingbacks and trackbacks (2)+

Add comment