How to create and navigate a directory with a single command

Most often when I am creating a directory in PowerShell I also want to navigate into that folder. I can do that on a single line like this.

New-Item -Path temp -ItemType Directory; Set-Location -Path temp

Or using aliases

md temp; cd temp

But that is just too much typing. So, I decided to create a function named mcd for make and change directory and have it loaded each time I start PowerShell. 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 I created a new file and changed the language mode from Plain Text to PowerShell. Copy and paste the code below into the file.

function mcd {
      [Parameter(Mandatory = $true)]

   New-Item -Path $Path -ItemType Directory

   Set-Location -Path $Path

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 mcd to create and navigate into a folder.

PS C:\> mcd test

    Directory: C:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2/3/2018  11:41 AM                test

PS C:\test>

Comments (4) -

  • Thomas Levesque

    3/4/2018 7:18:57 PM | Reply

    I use a different approach that's a bit more verbose, but doesn't require a script. md returns the directory object, so I just pipe it into cd :

    > md test | cd

    • Octave Muhirwa

      3/26/2019 7:57:19 PM | Reply

      Nice. Feeling too lazy to put the function into my powershell profile now lol. Will do this going 4ward, until it's too cumbersome at which point I'll use Donovan's function.

      Thanks for sharing Donovan Smile.

  • Adrian Enriquez

    8/7/2018 8:05:08 PM | Reply

    When I do that and open a new PowerShell session, I get this:

    . : File C:\Users\<me>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded. The file
    C:\Users\<me>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 is not digitally signed. You cannot run
    this script on the current system. For more information about running scripts and setting execution policy, see
    about_Execution_Policies at https:/
    At line:1 char:3
    + . 'C:\Users\<me>\Documents\WindowsPowerShell\Microsoft.PowerShell ...
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : SecurityError: (Smile [], PSSecurityException
        + FullyQualifiedErrorId : UnauthorizedAccess

    • Donovan

      8/7/2018 8:30:41 PM | Reply

      Sorry if it was not clear but "me" should have been replaced with your username on the machine. When you dir into users whatever folder maps to you is what should be there instead of the word "me".

Pingbacks and trackbacks (3)+

Add comment