Weekend Scripter: Back Up Your PowerShell Profile


Summary: Ed Wilson, Microsoft Scripting Guy, talks about automatically backing up your Windows PowerShell profile.

Microsoft Scripting Guy, Ed Wilson, is here. My rule for making backups is that when the loss of the item would cause me grief, it is backed up. Otherwise, there is not too much need for a backup. This includes things that are not related to computers in addition to computers, files, and Windows PowerShell profiles.

For example, if I go on a trip and I only have a single battery for my camera, would I be upset if that battery dies, drains, or otherwise becomes useless? Maybe. But then again, maybe not. On a recent trip to a local garden, I did not feel compelled to bring backup batteries because if the battery drained, died, or otherwise became inoperable, it was not big deal. I could come back another day. But when we went to Europe last year, we brought a backup camera in addition to three backup batteries, and backup CompactFlash cards.

When I begin to add a lot of cool stuff to my Windows PowerShell profile, it is time to think about backing it up. I mean, the entire point of creating a cool Windows PowerShell profile is to make things easier; so by definition, the absence of said profile would make things harder.

I am also a firm believer in avoiding manual processes. Whenever I see something that begins with “instruct the user to do…” or “manually do…,” I know it is the wrong answer, whether it is on a certification exam or in real life.

I want an automatic backup of my Windows PowerShell profile every time I open the Windows PowerShell console. In this way, any change I make to the profile is automatically kept. The next thing to do would be to copy those backups to online storage, but that is a question for later.

A PowerShell profile is simply a file

The great thing about a Windows PowerShell profile is that it is simply a text file. This means that I can easily copy it anywhere I want to, and call it whatever I want to. Restoring the profile is another matter—but again, it is something for a later discussion.

For now, I can use Copy-Item to copy the Windows PowerShell profile to a location of my choice. I also need to come up with a name scheme to avoid overwriting the other Windows PowerShell profile backups. Luckily, I use the function I created yesterday in Replace One PowerShell Function with Another to remove invalid file characters, to help create my backup file name.

I first need a location for my profile backups. To do this, I create a variable named BackupHome. I first check to see if the variable is being used, and if not, I create it. I place my profile backups in my Windows PowerShell home folder that resides in my Documents folder. Here is the code I use to create my new variable:

if(!(Test-Path variable:backupHome))

{

 new-variable -name backupHome -value "$doc\WindowsPowerShell\profileBackup"

}

It does, of course, rely on this variable (which I created previously) that points to my Documents folder:

New-Variable -Name doc -Value "$home\documents"

Back up the file

Now that I have a variable to point to my backup path, I need to back up the profile. To do that, I check to make sure that I have a new folder to store the backup. I then get the date in a sortable format and create the file name.

The file name is made up of four pieces:

  • Computer name
  • User name
  • Date
  • Last portion of the profile name

I then copy it to the backup folder. This is the complete backup profile function:

Function BackUp-Profile

{

 Param([string]$destination = $backupHome)

  if(!(test-path $destination))

   {New-Item -Path $destination -ItemType directory -force | out-null}

  $date = Get-Date -Format s

  $backupName = "{0}.{1}.{2}.{3}" -f $env:COMPUTERNAME, $env:USERNAME,

   (rifc -stringIn $date.ToString() -replacementChar "-"),

   (Split-Path -Path $PROFILE -Leaf)

 copy-item -path $profile -destination "$destination\$backupName" -force

I can then check to see if the backups are being created. To do this, I use my $backuphome variable, and select the last three copies:

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (1)

  1. James A. Brown says:

    I don’t know if this is a PowerShell v3.0 vs 4.0 thing, but…

    On my system, when capturing the date using the -s format in PS v3.0, the result is something like:
    2015-07-29T07:35:05

    When later trying to copy the profile into my backup folder, those colons inside the date variable will result in an invalid path name error. Your screen shots have the hours, minutes, and seconds separated by dashes, which are legal characters in file names.
    Colons are not.

    My fix was to use a custom format for capturing the date:
    $date = Get-Date -Format M-d-yyyy

Skip to main content