PowerShell and Profiles

I showed off my PathUtils module in my previous article. Today I’m going to show off my profile. Every time you execute a PowerShell prompt or start up ISE, four profiles get run.

The four profiles are:

> $Profile.AllUsersAllHosts

> $Profile.AllUsersCurrentHost

> $Profile.CurrentUserAllHosts

> $Profile.CurrentUserCurrentHost

When you edit or run $profile you actually edit or run the last one. I put most of my profile in the third one – profile.ps1. This is common to both the PowerShell prompt and the ISE. Then I only need to put the differences in the ISE or PowerShell prompt one.

Let’s take a look at my Microsoft.PowerShellISE_profile.ps1 file first:

Set-Location H:

function edit {
    param($file, $force = $false);

    if ($force) {
        if (-not (Test-Path $file)) {
            New-Item -ItemType File -Path $file

    psedit $file

My “home” is on my H: drive – it’s a Synology Diskstation in my basement. I change the location of documents, etc. to it and then sync the contents so that they are available off-line if I am using a laptop. Then I define a function “edit” – this creates the file I want to edit if it doesn’t exist (and I use -force) and then opens it in the ISE editor.

My Microsoft_PowerShell_profile.ps1 is similar:

Set-Location H:

Set-Alias edit atom

Instead of the ISE Editor, I’m using atom. Aside from that, this also doesn’t do much. All the work of my profile is done in profile.ps1. Here is the top of it:

Import-Module PathUtils

Add-Path -Directory "${env:ProfileFiles(x86)}\PuTTY"
Add-Path -Directory "${env:USERPROFILE}\AppData\Roaming\npm"

This just sets up my Path. I’ve added PuTTY and the NPM area to my path. Next comes git setup:

Import-Module posh-git

Add-Path -Directory "${env:ProgramFiles(x86)}\Git\bin"

function global:prompt {
    $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
    Write-Host($pwd.ProviderPath) -nonewline
    return "> "

Start-SshAgent -Quiet

posh-git was introduced last time. It’s a module that provides a colorized prompt when you are in a git repository. This is very useful when it comes to development.

I then have a series of functions that have helped me as a developer:

function Edit-HostsFile {
    Start-Process -FilePath atom -ArgumentList "${env:windir}\System32\drivers\etc\hosts"
function rdp ($ip) {
    Start-Process -FilePath mstsc -ArgumentList "/admin /w:1024 /h:768 /v:$ip"
function tail ($file) {
    Get-Content $file -Wait
function whoami {

function Get-ProcessorArchitecture {
    if ([System.IntPtr]::Size -eq 8) { return "x64" }
    else { return "x86" }

function Test-Port {



    $outputobj = New-Object -TypeName PSobject
    $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostName -Value $Target
    if (Test-Connection -ComputerName $Target -Count 2) {
        $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value "ONLINE"
    } else {
        $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value "OFFLINE"
    $outputobj | Add-Member -MemberType NoteProperty -Name PortNumber -Value $Port
    $Socket=New-Object System.Net.Sockets.TCPClient
    $Connection.AsyncWaitHandle.WaitOne($timeout,$false) | Out-Null
    if($Socket.Connected -eq $true) {
        $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value "Success"
    } else {
        $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value "Failed"
    $Socket.Close | Out-Null
    $outputobj | 
        Select TargetHostName, TargetHostStatus, PortNumber, Connectionstatus | 
        Format-Table -AutoSize

All of these came from someone else. I recommend poshcode.org – it’s got lots of good scripts in there that you can include in your profile.

The other thing to note about PowerShell is that there are lots of modules available. Some of them are really obvious – for example, if you are using Azure then you will want the Azure cmdlets which are contained in – you guessed it – the Azure module. However, I have a couple of modules I have found that are particularly useful.

  1. posh-git improves your git experience
  2. PowerShell Community Extensions (or PSCX) is a collection of useful cmdlets
  3. Carbon is another collection, targeted at devops

Funnily enough, I don’t tend to put pscx and carbon on my dev machines. However, I have an active lab build environment and those two modules end up on every Windows production box as part of the build.

I’m sure I could make my life even easier in PowerShell as a developer. However, this profile and these modules provide an excellent base for my continuing development work.