PowerShell Profiles

I rely pretty heavily upon PowerShell profiles to make my life easier. Similar to how you can use .bashrc to set aliases, define basic functions, and change your default prompt - Microsoft.PowerShell_profile.ps1 located in the %userprofile\Documents\WindowsPowerShell folder can alter your running powershell.exe environment.

Auto-Loading Modules

I like to automatically load a few commonly used modules at start up, especially the VMware PowerCLI and Quest Active Directory modules. This is most useful for the VMware portion - otherwise I would have to load up PowerCLI every time and that's annoying.

$questPresent = $powerclipresent = $true
try { .  
    add-PSSnapin  quest.activeroles.admanagement -ErrorAction Stop
} catch {
    Write-host "Quest Active roles not installed" 
    $questpresent = $false
}
try {  
    add-pssnapin  VMware.DeployAutomation       -ErrorAction Stop
    add-pssnapin  VMware.ImageBuilder           -ErrorAction Stop
    add-pssnapin  VMware.VimAutomation.Cloud    -ErrorAction Stop
    add-pssnapin  VMware.VimAutomation.Core     -ErrorAction Stop
    add-pssnapin  VMware.VimAutomation.License  -ErrorAction Stop
    add-pssnapin  VMware.VimAutomation.Vds      -ErrorAction Stop
} catch {
    write-host "VMware PowerCLI not installed"
    $powerclipresent = $false
}

Aliases/Functions

Some quick aliases/functions to replicate Linux functionality.

New-Alias sign Set-AuthenticodeSignature  
New-Alias which get-command  
New-Alias pop pop-location  
New-Alias push push-location  
if ((test-path "c:\Program Files (x86)\Vim\vim73\vim.exe")){  
    New-Alias vi "c:\Program Files (x86)\Vim\vim73\vim.exe"
}
if ((test-path "c:\Program Files\Vim\vim73\vim.exe")){  
    New-Alias vi "c:\Program Files\Vim\vim73\vim.exe"
}
New-Alias -name sudo 'c:\scripting\git-code\powershell\ps_misc\sudo.ps1'

function whoami { (get-content env:\userdomain) + "\" + (get-content env:\username); }  

Since I do a bit of script creation outside of PowerShell, It's nice to be able to standardize the headers with PS Help best practices (Synopsis, Description, Parameter, etc). The function below let's me run prep-PSScriptComments .\newscript.ps1 to automatically prepend the necessary comments.

function prep-PSScriptComments{  
    param($file)
    BEGIN {
        $content = Get-Content $file
        $psPrep = "<#`n`t.SYNOPSIS`n`t`n`t.DESCRIPTION`n`t`n`t.EXAMPLE`n`t`n`t.PARAMETER`n`t`n`t.NOTES`n`t`Author: Brian Marsh`n`tVersion: 1.0`n#>`n"
    }
    PROCESS {
        $psPrep | Set-Content $file
    }
    END {
        $content | Add-Content $file
    }
}

Go Shortcuts

Rather than remembering full paths, I like to set some "go" shortcuts (go home, go scripts, etc) that let me bounce around to various directories with ease.

# 'go' command and targets
if( $GLOBAL:go_locations -eq $null ) {  
    $GLOBAL:go_locations = @{};
}

function go ([string] $location) {  
    if( $go_locations.ContainsKey($location) ) {
        set-location $go_locations[$location];
    } else {
        write-output "The following locations are defined:";
        write-output $go_locations;
    }
}

$go_locations.Add("home", "~")

if (test-path $($env:USERPROFILE+"\Downloads")){  
    $go_locations.Add("dl", $env:USERPROFILE+"\Downloads")
}
if (test-path "c:\scripting\git-code\powershell") {  
    $go_locations.Add("scripts", "c:\scripting\git-code\powershell")
}
if (test-path $($env:USERPROFILE+"\Documents\My Box Files")) {  
    $go_locations.Add("box", $env:USERPROFILE+"\Documents\My Box Files")
}
$profilePath = ([System.IO.FileInfo] $profile).DirectoryName
$go_locations.Add("profile", $profilePath)

Full Source

My full, barely altered profile can be found on Github.

Tagged in: PowerShell
comments powered by Disqus