Articles

Are you following me? PowerShell everywhere, with SkyDrive. How to keep your files in Sync.

In Module, PowerShell, PSDrive, PSModulePath, PSProvider, SkyDrive on August 30, 2012 by brwilkinson

 

A picture speaks 1000 words, so here is an over view about the next 5 posts on this topic.

image

If you want to have all of your scripts and PowerShell Modules at your fingertips all the time, then this is a possible solution. You can also have roaming default settings for any CmdLet that you like with PSDefaultParameterValues.

Install SkyDrive

To get started install SkyDrive on all of your machines, including Virtual Machines etc or where ever you write code. E.g. Laptop, Desktop and Virtual Desktop.

Setup SkyDrive to Sync from a particular directory on each machine. The location does not have to match on each Computer. You can install it on the drive that has the most space or best suits your needs. This also gives you the ability to move the root folder at a later stage with only a single configuration change within PowerShell.

Setup the directories

Within the SkyDrive folder hierarchy create a directory called PS:\ for your PowerShell Scripts.

–> Within the PS Directory create a directory called MODS.

–> After that create or move your own or downloaded Modules into the *\SkyDrive\PS\MODS\ directory.

–> Within the PS Directory also create a directory called PS-Help E.g. *\SkyDrive\PS\PS-Help

–> Within the PS Directory also create a directory called Profiles-Backup E.g. *\SkyDrive\PS\Profiles-Backup

You will now have this directory on every system that you work on, once the Directories are synched (via SkyDrive).

Setup PowerShell

Now you need to prepare PowerShell recognize the Directories and we will leverage a Profile for this as well as a PSDrive.

I personally am the only user on the Computers that I work on, so I use the $profile.AllUsersAllHosts profile, which is located in C:\Windows\System32\WindowsPowerShell\V1.0\Profile.ps1

However if you share your computer, you may well prefer the $profile.CurrentUserAllHosts profile, which is located in: C:\Users\user123\Documents\WindowsPowerShell\profile.ps1

Noting that the profile that you use on each machine can be different.

So then I create and edit that profile file, the profile is the only thing that is not synched across all of the Computers, so you create and edit the file on each system. They will all be very similar, however contain different paths for the SkyDrive root folder, depending on the placement on each system.

Speaking of Profiles, as a general recommendation, if you keep your personal code within functions within you own Personal Module and not within the Profile it should be preferable, since that way you can be sure your code roams with you on all machines, rather than being stuck in a static profile.

So speaking of the Profile, this the the likely contents that should go in there and will get you up and running to Sync your data and access your synched code, modules and settings.

C:\Windows\System32\WindowsPowerShell\V1.0\Profile.ps1

# Set the error colour (personal preference)            
(get-host).PrivateData.ErrorForegroundColor = 'DarkRed'            
(get-host).PrivateData.ErrorBackgroundColor = 'White'
 
# This outputs errors as below.
image
 
# Define the path to the SkyDrive Root (this can be different on all machines)            
New-Variable -Name SkyDrive -Value "M:\SkyDrive" -Scope Global -Force            
            
# Create a new PSDrive to the SkyDrive Root, then change to that location            
New-PSDrive -Name PS -PSProvider FileSystem -Root $SkyDrive\PS -EA 0 | select Name, Root            
Set-Location -Path PS:            
            
# Add a variable for the new Module Directory            
New-Variable -Name MyMods -Value "PS:\Mods" -Scope Global -Force            
            
# Add the new Module path to the Global PSModulePath search path            
# This step is required so that when you run Get-Module -List available it will search the new location.
$env:PSModulePath = (($env:PSModulePath + ";" + $MyMods) -split ";" | Get-Unique) -join ";"            
            
# I Import my personal Module at starttime            
Import-Module BRW -Force -Global            
            
# Create a shortcut to reload the personal Module            
function Import-BRW {import-module brw -force -global -Verbose}             

# Function (shortcut) to edit the profile            
function Edit-Profile {psedit $profile.AllUsersAllHosts}                                               
            
# Backup local profile            
Copy-Item -Path $profile.AllUsersAllHosts -Destination PS:\Profiles-Backup\$ENV:Computername-profile.txt -Force            
            
# Custom hello message            
"There is " + ([math]::round((((date "10/26/2012") - (date)).TotalDays))) + " days till Microsoft Surface is Released . . "            

Summary

Okay that will get you up and running. To summarize, you now have your PSDrive called PS: that will be pointing to the PS Directory in the root of your SkyDrive location, this is a FileSystem PSProvider. The data within the drive will be the same across all of your machines, thanks to SkyDrive.

You have your Modules in the PS:\MODS\*All Modules Go Here*

You have your own Personal Module there as well. E.g. I have a module called BRW. PS:\MODS\BRW. We can cover more on this in a later step.

Anytime you run Get-Module -ListAvailable you can see the modules in your directory on all of your machines.

Leave a comment