Contents

Create Your Own PowerShell Profile

Being a fan of automation I like to create my own PowerShell profile. It enables me to load various settings that normally take more time.

The PowerShell profile resides in your home directory and if you work in an AD environment with roaming data you’ll have the same profile on every computer. PowerShell profiles are not new and dates back to PowerShell v2.0.

Others people have written about this subject before but I wanted to share my take on it.

Create the profile

To check if you have a profile execute the following command:

1
Test-Path $profile

/posts/2017/create-your-own-powershell-profile/170619-posh_profile_test.png

If this returns false it means you don’t have a profile and you have to create one. The easiest way to do that is by executing the following:

1
New-Item -Path $profile -type file force

/posts/2017/create-your-own-powershell-profile/170619-posh_profile_create.png

From this point on you can put in anything that helps you speed up.

PowerShell drives

I have several directories which I use regularly. Instead of having to type the entire path I can create a PowerShell drive.

1
2
3
4
# Create drives
New-PSDrive -Name Git -PSProvider FileSystem -Scope Global -Root "C:\\Users\\User\\OneDrive\\\_Git" | Out-Null

New-PSDrive -Name Development -PSProvider FileSystem -Scope Global -Root "C:\\Users\\User\\OneDrive\\\_Development" | Out-Null

/posts/2017/create-your-own-powershell-profile/170619-posh_profile_psdrive_example.png

Now instead of having to type the entire path I can simply use the new drive. This not only speeds up the way you enter your directory but also simplifies the reference to your current working directory.

Credentials

The next thing I do is when I’m part of a domain I usually need another user account to access my database servers. So I call the next piece of code to enter the credentials for my administrative account:

1
2
3
4
# Ask for a credential for later use
if ((Get-WmiObject Win32\_ComputerSystem).PartOfDomain) {
    $Credential = Get-Credential
}

/posts/2017/create-your-own-powershell-profile/170619-get-credential_window.png

You can reference the $Credential parameter to other functions that need more privileges. For instance, if you use dbatools, a lot of the commands have this parameter to make sure you connect to the SQL Server instance with the right credential.

Color schemes

Color schemes are important. There have been studies that show that certain fonts and colors make working on a console easier for the eyes. I don’t like the colors for errors and warnings and if I have to go through a lot of them it’s hard to read the bright red on the blue background.

1
2
3
4
5
6
7
# Console color schemes
$console = (Get-Host).UI.RawUI

$host.PrivateData.ErrorBackgroundColor = "Red"
$host.PrivateData.ErrorForegroundColor = "White"
$host.PrivateData.WarningBackgroundColor = "DarkCyan"
$host.PrivateData.WarningForegroundColor = "Yellow"

/posts/2017/create-your-own-powershell-profile/170619-posh_profile_color_example.png

At least I can read this better than the original color scheme. Play around with this and you’ll see it makes handling messages from the console a lot easier.

Window and buffer size

It’s possible to change these values to make sure all your data fits inside your console.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Set the buffers 
$size = $console.BufferSize
$size.Height = \[Math\]::Max(2000, $size.Height)
$size.Width = \[Math\]::Max(150, $size.Width)
$console.BufferSize = $size

# Set the window size
$size = $console.WindowSize
$size.Height = 50
$size.Width = 150
$console.WindowSize = $size

Setting the location

For me, it’s not always a good thing that the working directory is set to my home folder. This happens by default when you open a PowerShell console. If you open it with administrative privileges it points to the system32 folder of your Windows directory.

1
2
# Make sure the profile starts in the root of the C-drive
Set-Location C:\\

Conclusion

In the end, you can load all kinds of settings in your profile to make your life a little easier. From the settings above to creating aliases, starting other scripts, etc. I hope this was informative for you and maybe you’ll get started with profiles too.