PowerShell Profiles – The structure of your _PSH_BASE.ps1 file

In my previous post PowerShell Profiles – The profile.ps1 file I showed you my profile.ps1 file. In this post, I’ll show you a way to structure your base file, so that you can use it for your functions and aliases. Make sure that you always use max 2 files. 1The first file is your profile.ps1 file and the other file is this _PSH_BASE.ps1 file. If you use like 3 or 4 files, it can take a couple of seconds to load your PowerShell session.

Start with the header:

######################################################################################### 
# File Name is: _PSH_BASE.ps1
######################################################################################### 
# Powershell Profile Base - Updated:30/08/2015 @ 14:00
#########################################################################################

It contains some basic stuff, but it’s not necessary.

# Set the Home Directory for Powershell to start in 
Set-Location $env:ScriptHome

This part will start your PowerShell session in your script home. More information about this environment variable in my previous blog post.

## Profile Functions ###################################
#region Functions
# PLACE FUNCTIONS HERE
#endregion

Because I have a lot of functions in my PowerShell Profile, it’s great to use a region so that you can hide your code with the + sign in PowerShell ISE like this:

PowerShell Regions

## Profile Aliases ################################### 
Set-Alias -Name list -Value dir/w | Out-Null

This is the place where you can define your profile aliases. For example, if you type “list” in the console, it will do the “dir/w” command in the background.

## Only PowerShell ISE ###################################
if ($host.name -eq 'Windows PowerShell ISE Host')
{
    Enable-ScriptBrowser
}

I’ve downloaded the ScriptBrowser plugin for ISE and I want it to run only in the PowerShell ISE console. Here you can place functions or commands that should only run in PowerShell ISE. Use “ConsoleHost” instead of “Windows PowerShell ISE Host” to define commands and functions to run only in the console.

## Only PowerShell Console ###################################
if ($host.name -eq 'ConsoleHost')
{
    $a = (Get-Host).PrivateData 
    $a.ErrorForegroundColor = "RED" 
    $a.ErrorBackgroundColor = "WHITE" 
    $a.WarningForegroundColor = "YELLOW" 
    $a.WarningBackgroundColor = "BLACK" 
    $a.DebugForegroundColor = "YELLOW" 
    $a.DebugBackgroundColor = "BLACK" 
    $a.VerboseForegroundColor = "YELLOW" 
    $a.VerboseBackgroundColor = "BLACK" 
    $a.ProgressForegroundColor = "YELLOW" 
    $a.ProgressBackgroundColor = "DARKBLUE"

    $Shell = $Host.UI.RawUI
     $size = $Shell.BufferSize
     $size.width=150
     $size.height=500
     $Shell.BufferSize = $size
     $size = $Shell.WindowSize
     $size.width=150
     $size.height=50
     $Shell.WindowSize = $size
}

I use the above code to change the error colors in the PowerShell Console. This doesn’t work in PowerShell ISE, that’s why I used the if ($host.name) command.

# Let the user know your done
$ConsoleType = $Host.Name
Write-Host -ForegroundColor "Green" "Base profile for $ConsoleType loaded."

Last but not least, let the user know that the Base profile is loaded for the ISE or Console.

In the next blog post, I’ll show you some great functions I use with my PowerShell Profile. Cheers!

One thought on “PowerShell Profiles – The structure of your _PSH_BASE.ps1 file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s