Summary: Learn how to copy Windows PowerShell profiles from your computer to SkyDrive.
Microsoft Scripting Guy, Ed Wilson, is here. June Blender is joining us again today with another guest blog post.
Like many avid Windows PowerShell users, I'm pretty protective of my Windows PowerShell profiles. After all, they are my primary workhorses. All of the aliases and functions that I depend on to automate my life live in those profiles. When I set up a new machine, one of the first things I do is to copy my profiles to the new machine. So the thought of losing a profile is a pretty grim one.
Now that my machines are running Windows Server 2012 R2 and Windows 8.1, I've become a SkyDrive fan. I use vanilla SkyDrive for my personal files and SkyDrive Pro for my work files. So, it's only natural that I want to copy my profiles to SkyDrive.
I wrote a little script to do that and I run it as a Windows PowerShell scheduled job. I've shared it in the TechNet Code Gallery, so you can improve it and use it: Copy-ProfilestoSkyDrive.
A few caveats…
- To copy your profiles to SkyDrive, you must have SkyDrive configured and available on the local machine. I set up my SkyDrive once on one machine with my Outlook.com account, and now it's set up everywhere. If you still need to configure yours, here are the instructions: SkyDrive overview. When you set it up, by default, it creates a virtual directory on all of your machines at $home\SkyDrive, that is:
$env:UserProfile\SkyDrive, typically, C:\Users\<UserName>\SkyDrive
Try it:
PS C:\> Test-Path $home\SkyDrive
True
- If SkyDrive isn't configured, or the virtual directory is in a different location, you can use the SkyDrivePath parameter to direct the script to the correct destination.
- To copy files to your SkyDrive, you can use Save As in Windows. In Windows PowerShell, you can use Copy-Item, BitsTransfer, and programs such as Robocopy. For text files, like your profiles, Copy-Item works great, so I use it in my script.
Copy profiles to SkyDrive
The script is pretty simple. It gathers your profile files and copies them to a subdirectory in your SkyDrive directory. The default subdirectory name is PSProfiles, but you can use the Subdirectory parameter to specify a different subdirectory name.
If you have AllUser and CurrentUser profiles, it puts the AllUsers profiles in an AllUsers subdirectory and the CurrentUser profiles in a CurrentUser subdirectory. Otherwise, it skips the second layer of subdirectories.
The default command copies your profile to the PSProfiles subdirectory in SkyDrive. I'll use the Verbose switch to see what it's doing:
PS C:\> .\Copy-ProfilesToSkyDrive -Verbose
The script copied only my CurrentUser profiles, because that's all I have.
VERBOSE: Copied C:\Users\juneb\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 to
C:\Users\juneb \SkyDrive\PSProfiles
VERBOSE: Copied C:\Users\juneb\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 to
C:\Users\juneb \SkyDrive\PSProfiles
VERBOSE: Copied C:\Users\juneb\Documents\WindowsPowerShell\profile.ps1 to
C:\Users\juneb\SkyDrive\PSProfiles
I keep only my latest copy of each profile, but the Scripting Guy likes to keep multiple copies, so I added a TimeStamp switch parameter. The TimeStamp parameter appends a time stamp with the current time to the directory name. So you can keep multiple copies of your profiles without changing the profile file name.
PS C:\> dir $home\SkyDrive\PSProfile*
Directory: C:\Users\juneb \skydrive
Mode LastWriteTime Name
---- ------------- ----
d---- 10/9/2013 3:58 PM PSProfiles-2013-10-09T15:58:40.0323699-07:00
d---- 10/10/2013 6:23 AM PSProfiles-2013-10-10T06:23:20.5176836-08:00
Copy profiles to remote computer
Now all of your profiles safe. When you get a new machine, you probably want to copy the profiles to the new machine so they're available in your sessions.
If you're using a Windows PowerShell remote session to copy the profiles, be sure to use the fully qualified profile path as the destination. The $profile automatic variable does not work in remote sessions:
PS C:\> $s = New-PSSession -ComputerName Server01
PS C:\> Invoke-Command -Session $s {$profile}
PS C:\>
PS C:\> Invoke-Command -Session $s {$profile.CurrentUserCurrentHost}
PS C:\>
To copy the profile, use a command like this one:
PS C:\> $prof = Get-Content $home\SkyDrive\PSProfiles\Microsoft.PowerShell_profile.ps1
-or-
PS C:\> $prof = Get-Content $profile
Then, use the local $prof variable in your remote session.
PS C:\> Invoke-Command -Session $s {Set-Content -Value $Using:prof -Path $home\Documents\WindowsPowerShell\ Microsoft.PowerShell_profile.ps1}
This command uses the $Using scope identifier to indicate that the $prof variable is in the local session. Otherwise, Windows PowerShell would look for it in the remote session.
I hope this is useful. If you have comments or suggestions for improving the script, leave comments here or in the TechNet Code Gallery.
~June
Thanks to June for another great post. 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