Creating a PowerShell Profile Repository

2 minute read

For day 11 of 100 Days of Code, I’m going to create a new repo to host my PowerShell Profile https://github.com/Hawzy/powershell-profile. The profile gets auto-loaded when you launch the PowerShell Console depending on your user context. So I’m going to populate it with useful commands, modules, configs, themes and cmdlets so that whenever I set up a new computer I can copy down my settings and be ready to go. You can see the location of the different profile types by running $profile | select *

Resources

Couple links and profiles that had some good stuff in them.

Create Github Repo

I had already created the repo on the webpage of github.com so it already exists we just need to sync our local repo.

  1. mkdir C:\github\powershell-profile
  2. git init
  3. code C:\github\powershell-profile\profile.ps1 && code C:\github\powershell-profile\readme.md
  4. git remote add origin https://github.com/Hawzy/powershell-profile.git

Structure

I think this is a great way to keep organized, seperate each function by type, and then load each seperate type file. Save in $profile

For my purposes I’m using the CurrentUserAllHosts : C:\Users\nickh\OneDrive\Documents\PowerShell\profile.ps1 Profile and putting all these files directly into my Onedrive

Directory Structure

$profileDir = $PSScriptRoot;

foreach ( $includeFile in ("azure", "defaults", "openssl", "development") ) {
    Unblock-File $profileDir\$includeFile.ps1
. "$profileDir\$includeFile.ps1"
}

Add functions

Found this while searching around, this will save me a ton of time and keystrokes, I’ll drop it in the development.ps1, as well as my New-BlogPost function I made a couple posts ago.

function Invoke-GitAddCommit {
    <#
    .SYNOPSIS
        Shortcut for adding and committing files using git.
    .EXAMPLE
        Invoke-GitAddCommit "initial commit"
        gac "my commit" .\myFile.ps1
    #>
    [Alias("gac")]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Message,
        [Parameter(Position = 1)]
        [string]
        $File
    )
    if ($File) {
        git add $File
    }
    else {
        git add .
    }
    git commit -m $Message
}

Copy to profile directory

Once profile.ps1 and all my other supporting .ps1 files are in place I can copy them to my profile directory using this command: copy-item -Path C:\github\powershell-profile\* -D C:\Users\nickh\OneDrive\Documents\Powershell\ -Recurse

Conclusion

Now I just need to reload the profile and test the new function on this blogpost gac "day 11" && git push. If you are reading this, then it looked like it worked. For Day 11 of 100 days of code, I created the structure of my PowerShell Profile and added a couple functions to it. I put it in OneDrive so it should now sync to all my machines that are setup that way, but I can also pull a copy from GitHub if I need to. You can view the GitHub repo here: https://github.com/Hawzy/powershell-profile

Leave a comment