Contents

PowerShell Profiles - The structure of your _PSH_BASE.ps1 file

Contents

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: [code language="powershell”] ######################################################################################### # File Name is: _PSH_BASE.ps1 ######################################################################################### # Powershell Profile Base - Updated:30/08/2015 @ 14:00 ######################################################################################### [/code] It contains some basic stuff, but it’s not necessary. [code language="powershell”] # Set the Home Directory for Powershell to start in Set-Location $env:ScriptHome [/code] This part will start your PowerShell session in your script home. More information about this environment variable in my previous blog post. [code language="powershell”] ## Profile Functions ################################### #region Functions # PLACE FUNCTIONS HERE #endregion [/code] 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 [code language="powershell”] ## Profile Aliases ################################### Set-Alias -Name list -Value dir/w | Out-Null [/code] 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. [code language="powershell”] ## Only PowerShell ISE ################################### if ($host.name -eq ‘Windows PowerShell ISE Host’) { Enable-ScriptBrowser } [/code] 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. [code language="powershell”] ## 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 } [/code] 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. [code language="powershell”] # Let the user know your done $ConsoleType = $Host.Name Write-Host -ForegroundColor “Green” “Base profile for $ConsoleType loaded.” [/code] 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! [polldaddy poll=9049601]