Use symlinks to version-control your PowerShell profile with Git

PowerShell is a versatile command-line shell and scripting environment that empowers system administrators and developers to automate tasks and manage systems more efficiently. One of PowerShell's features is its profile, a script that runs when you start a PowerShell session. Storing your PowerShell profile in a Git repository is an excellent option when working across multiple systems or when you want to version-control your setup. This article discusses using symbolic links to manage your PowerShell profile with Git.

Saving the digital universe, one line of PowerShell at a time.

Understanding PowerShell profiles

A PowerShell profile is a script that PowerShell runs at startup. It can contain elements you want available in every PowerShell session. PowerShell supports several profiles, but your current user's current host profile is the most common.

Why use Git for your profile

Storing your PowerShell profile in Git has several benefits:

  • Version control: You can track changes to your profile script over time, which is invaluable for understanding the history of your modifications and for recovery in case of an error.
  • Consistency: It allows you to synchronize your profile across multiple machines, ensuring a consistent environment everywhere you use PowerShell.
  • Collaboration: If you're working with a team, sharing a PowerShell profile can help standardize the working environment.

Symbolic links (symlinks) are a feature of the file system that creates a type of shortcut to a file or directory. Using a symlink, you can store your profile in a Git repository and link to it from the default PowerShell profile path. PowerShell can access the profile as if it were in its original location while you maintain the actual file in a version-controlled repository.

  1. Create a Git repository: If you haven't already, set up a Git repository where you'll store your PowerShell profile. You can create a new repository on services like GitHub, GitLab, or Bitbucket and then clone it locally to your computer.

  2. Move your existing profile: Move your current PowerShell profile to the local clone of your new Git repository. If you don't have a profile yet, create one in the repository folder.

  3. Create a symbolic link to your profile: Use the following example to create a symbolic link. Creating a symlink requires launching PowerShell elevated as an administrator if you're using Windows. Store the path to your Git repository folder in the $gitRepoPath variable.

    Note: If you're running PowerShell as a different user when launching it elevated as an admin on Windows, use actual paths instead of $PROFILE or $HOME. These built-in variables point to user's profile that's running PowerShell.

    1$symlinkParams = @{
    2  Path = $PROFILE
    3  Value = "$gitRepoPath/PowerShell/Microsoft.PowerShell_profile.ps1"
    4  ItemType = 'SymbolicLink'
    5  Force = $true
    6}
    7New-Item @symlinkParams
    
    1    Directory: /Users/mikefrobbins/.config/powershell
    2
    3UnixMode   User Group                LastWriteTime            Size Name
    4--------   ---- -----                -------------            ---- ----
    5-rw-r--r-- mikefrobbins   staff    11/8/2023 17:59              97 Microsoft.PowerShell_profile.ps1 ->
    6                                                                   /PowerShell/Microsoft.PowerShell_profile.ps1
    
  4. Commit your profile to Git: Using the standard Git commands, add your profile to the repository and commit it.

    1git add Microsoft.PowerShell_profile.ps1
    2git commit -m 'Added PowerShell profile'
    3git push
    
     1[main aaa6434] Added PowerShell profile
     2  1 file changed, 107 insertions(+)
     3  create mode 100644 PowerShell/Microsoft.PowerShell_profile.ps1
     4
     5Enumerating objects: 5, done.
     6Counting objects: 100% (5/5), done.
     7Delta compression using up to 12 threads
     8Compressing objects: 100% (4/4), done.
     9Writing objects: 100% (4/4), 1.68 KiB | 1.68 MiB/s, done.
    10Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
    11remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
    12To https://github.com/mikefrobbins/SystemConfiguration.git
    13    fec6836..aaa6432  main -> main
    
  5. Clone and link your other systems: On your other systems, clone the repository and create a symbolic link. Now, you've synchronized your PowerShell profile across your machines.

Best practices

  • Backup before changes: Always back up your profile before making changes or updates to avoid any loss of important configurations.
  • Commit regularly: Make it a habit to commit changes to your profile as you make them, including meaningful commit messages to track your changes.
  • Test your profile: Ensure your profile scripts work as intended on new machines to avoid system-specific configurations that may not translate across different environments.

Summary

Using symbolic links with Git for your PowerShell profile is an excellent way to maintain a consistent, version-controlled environment. It simplifies managing and sharing profiles for users who want to leverage the power of PowerShell across various systems or in a team setting.

References

Acknowledgments

Thanks to Sean Wheeler for the tip about using symbolic links to store PowerShell profiles in a Git repo.