Pimp your PnP PowerShell

Sometimes you would like to have a bit more context when it comes to what and where you are using PnP PowerShell.

Well, actually you can, and it's not even that complex. Have a look at the following screenshot:

pimppnpps1

Notice the prompt:

[/sites/demo1] C:\repos\pnp-powershell [dev ≡]>

The first part of the prompt shows up when you are connected to a site. It will show the path of the site collection you connected to. The second part shows the current path you are in on your local computer. The third and last part shows which Git branch you are in and how many commits you have waiting to push, etc.

I use a module called posh-git for this. It's a simple install:

install-module posh-git

Then modify your PowerShell profile. The location of this file can be found simply by entering $PROFILE on a line of it's own. It will return the path of your profile.
If the file does not exist, create it, otherwise simply edit it.

Add this snippet at the top of the file:

import-module posh-git

function prompt {
    $prompt = "";
    if($env:PNPPSSITE -ne $null)
    {
    	$prompt = Write-Prompt "[$($env:PNPPSSITE)] " -ForegroundColor ([ConsoleColor]::Green)
    } 
    $prompt += & $GitPromptScriptBlock
    if ($prompt) { "$prompt" } else { " " }
}

Save the file, close PowerShell and reopen it. Use Connect-PnPOnline to connect to a site collection and you'll notice the path of the site collection in your prompt.

What your updated profile does is load the posh-git module, and then it will modify the default PowerShell prompt to include a bit more information. You'll notice we check for the value of the PNPPSSITE environment variable. When you connect to a site, the latest builds (it does this since a few months already) of PnP PowerShell sets two environment variables that you can use:

PNPPSHOST : this contains the name of your tenant, like mytenant.sharepoint.com
PNPPSSITE : this contains the path of the site collection you connected to, like /sites/mycompanysite

If the variable is not null we include the path of the site, otherwise we simply do not add it to the prompt.

It's as simple as that.

comments powered by Disqus