Synchronize your PowerShell Profile with OneDrive

by Bharat Suneja

I make frequent changes to my PowerShell profile and like to have the same PowerShell envirnoment on all computers that I use PS from. To accomplish this, I used to copy the PowerShell profile to a folder on OneDrive and copy it back to the WindowsPowerShell folder on other computers – first, manually, and then using a script. This required having to repeat the process each time I updated the profile on one computer.

But there’s an easier way to have a single copy of your PowerShell profile and store it on OneDrive. This method allows PowerShell to automatically use the latest profile version synchronized to OneDrive. If you make changes to the profile, you no longer have to worry about copying the updated local version to OneDrive (manually) or sync/copy the updated version from OneDrive to all computers.

If you’re thinking about how complicated this solution is going to be, you can discard that thought right now! If simple solutions make you happy, this should make you jump with joy!

The caveat with this approach, if it can be called that: changes AND deletions to your PowerShell profile are synced to all computers where you set it up — but that’s exactly the purpose of this automation effort.

Folder or file Default path
Windows user profile C:\Users\<username>
Windows PowerShell profile file C:\Users\<username>\Documents\WindowsPowerShell\
Microsoft.PowerShell_profile.ps1
New PowerShell 7 profile file.
See Migrating from Windows PowerShell 5.1 to PowerShell 7.
C:\Users\<username>\Documents\PowerShell\
Microsoft.PowerShell_profile.ps1
Microsoft OneDrive folder C:\Users\<username>\OneDrive

 
Here’s how it works:

  1. Install OneDrive The OneDrive app is included in Windows 10 and later. If you’re running an older version of Windows, download and install it.
  2. Create a WindowsPowerShell folder on OneDrive Navigate to the new OneDrive folder (the cached copy on your local drive) and create a WindowsPowerShell folder here.

    By default, the OneDrive app synchronizes your data to the OneDrive folder in your user profile (Default: C:\users\<username>\OneDrive). I change mine to a root level folder (for example: C:\Bharat\OneDrive) to make it easier to type.

  3. Rename the WindowsPowerShell folder in your user profile Check the Documents folder in your profile (typically C:\users\<username>\Documents). If you have a WindowsPowerShell folder (or PowerShell folder if you’re using PowerShell 7.x), rename it to something like WindowsPowerShell-BAK.
  4. Create a symbolic link Create a symbolic link called WindowsPowerShell (PowerShell 5.11) or PowerShell (PS 7.x) in the Documents folder and point it to the WindowsPowerShell folder in your OneDrive folder.

    If you’re using Widows PowerShell 5.1:

    New-Item -ItemType SymbolicLink -Path C:\users\<username>\Documents\WindowsPowerShell -Target C:\Bharat\OneDrive\WindowsPowerShell

    If you’re using PowerShell 7.x:

    New-Item -ItemType SymbolicLink -Path C:\users\<username>\Documents\PowerShell -Target C:\Bharat\OneDrive\WindowsPowerShell

    If you’re using both Windows PowerShell 5.11 and PowerShell 7.x, you can create both links and point them to the same folder on OneDrive. This ensures you’re using a single PowerShell profile for both versions of PowerShell. There’s one caveat: If you have some commands in your profile that only work on one version of PowerShell, they’ll fail when your start the other version where the commands aren’t supported.

    Note: If you’re on an older version of PowerShell and can’t use the New-Item cmdlet above to create a symbolic link, do this using the mklink command from a command prompt (cmd) with admin privileges.
    1. Navigate to the Documents folder in your user profile.
    2. Create the symbolic link.

      mklink /D WindowsPowerShell c:\Bharat\OneDrive\WindowsPowerShell

      If using PowerShell 7:

      mklink /D PowerShell c:\Bharat\OneDrive\WindowsPowerShell

  5. Rinse and repeat on your other computers.

    Tip: Before you create the symbolic link to the WindowsPowerShell folder on other computers, the folder should be synchronized from OneDrive to that computer. Make the WindowsPowerShell folder in your OneDrive available offline. Use Windows Explorer to browse to your local OneDrive folder > right-click the WindowsPowerShell folder and select Make available offline.

Now start PowerShell. If you have a PowerShell profile in the OneDrive folder, it’ll load by default. Any changes made to the profile will sync to the other computers.

{ 2 comments… read them below or add one }

True_Favor March 18, 2021 at 12:25 pm

How would I “unsynchronize” my OneDrive from running PowerShell scripts?

Reply

Bharat Suneja March 18, 2021 at 4:43 pm

1. Copy contents of the synced WindowsPowerShell (PowerShell 5.x) or PowerShell (PowerShell 7.x) folder to a temporary/alternate location on your local drive.
2. Remove the symbolic link (WindowsPowerShell or PowerShell) folder in c:\users\\documents (step 3 in article).
3. If you renamed the original WindowsPowerShell folder in c:\users\
\documents, rename it back to WindowsPowerShell (PowerShell 5.x) or PowerShell (PowerShell 7.x). Alternatively, create a new folder (WindowsPowerShell or PowerShell) for the PowerShell version you’re using.
4. Copy contents of the synced WindowsPowerShell folder from the alternate location to the WindowsPowerShell or PowerShell folder.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: