Use powershell profiles like a pro

If you do any software development in a Windows environment, you’ll be spending enough time inside the Powershell (PS) console that it will be worth customizing the PS Profile to speed up your workflow.

The PS Profile is essentially a .ps1 script that is run whenever a new PS console is opened. Without going too deep into how a sysadmin might use it, the average developer will probably want to use it to quickly do a few basic things:

  • customize the shell appearance
  • create shortcuts to favourite directories
  • automatically import custom .ps1 scripts that you wrote or downloaded.

Locating profile.ps1

The $PROFILE variable should return the path to the profile for the current user (the > symbol is used to denote console input):

> $PROFILE
C:\Users\chris\Documents\WindowsPowerShell\profile.ps1

Now that you know where Windows is looking to load the profile from, you can use your text editor of choice to create or modify it:

notepad $PROFILE

Let’s configure the profile

Add the below code to profile.ps1, which will be executed when a new console is opened.

Set the starting directory
# Navigate to the default startup folder
cd "C:\PATH\TO\YOUR\PREFERRED\STARTUP\FOLDER\"
Use a more succinct prompt

The default prompt contains the entire path to the current working directory which can take up a lot of space. To show only the name of the current folder, the default prompt function can be overwritten by adding:

# Show current directory (instead of entire path) in prompt:
Function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

This will change the prompt from this:

C:\a\really\long\path\to\the\current\working\directory>

to this:

directory>

This clears up the console, and if you ever need to check exactly where you are, just use Get-Location or it’s alis pwd.

Create shortcuts to favourite directories

I added the below functions to navigate to my development directory by just typing dev, or to my blog working directory by typing blog:

# cd to dev folder
function dev {
    cd "C:\Users\chris\webdev"
}
# cd to blog folder
function blog {
    cd "C:\Users\chris\webdev\myblog\blog"
}
Automatically import custom scripts

I prefer to store custom scripts that I have written or borrowed from others within my local development folder. This makes it easy to manage and keep synchronized with a git repository.

I added the below script to profile.ps1 to look inside this folder and import all .ps1 files:

# Import custom powershell scripts
$custom_script_dir = "PATH_TO_YOUR_CUSTOM_PS1_SCRIPT_DIRECTORY"
Write-Output `
 ("*** Importing all .ps1 modules from " `
 + $custom_script_dir + `
 " ***")
Get-ChildItem `
 $custom_script_dir `
 -Filter *.ps1 | `
 Foreach-Object `
 { Import-Module ($custom_script_dir + $_) }
Set the execution policy to allow scripts to run

According to the official docs, “PowerShell’s execution policy is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. This feature helps prevent the execution of malicious scripts.”

Press the Windows key and search for Powershell, right-click and select Run as administrator then enter the following commad:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Try it out

To try it out simply place some custom scripts in the directory specified above and open a new PowerShell instance.

If you want a useful script to try this out on, check out my script that searches Google from the powershell console.