If you like me uses PowerShell, if not every hour but at least every day. You end up tweaking the PowerShell profile, making your own aliases, functions and so on.
Now that is awesome, but wouldn’t it be nice to have it sync’d between all your devices?
I think so, so let’s put it in OneDrive 🙂
I hope this code is all self-explaining, just put it in your profile (Microsoft.Powershell_profile.ps1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# PS profile file name $PSProfileFileName = "Microsoft.Powershell_profile.ps1" # Get Onedrive path $OneDriveFoler = $(Get-ItemProperty HKCU:\SOFTWARE\Microsoft\OneDrive\ -Name UserFolder).userFolder # Where in OneDrive to store the file $CloudFolder = "$OneDriveFoler\Apps\PowerShellProfile\" # Get profile path $LocalPSProfilePath = "$env:USERPROFILE\Documents\WindowsPowerShell" # Find newest file, overwrite the older one $LocalFile = Get-Item "$LocalPSProfilePath\$PSProfileFileName" $CloundFile = Get-Item "$CloudFolder\$PSProfileFileName" # If cloud is newer If ( $CloundFile.LastWriteTime -gt $LocalFile.LastWriteTime) { Write-Output "Cloud profile is newer, overwriting local file...relaunch PowerShell to get newest version" Copy-Item $CloundFile -Destination $LocalPSProfilePath -Force -Confirm:$false } # If local is newer If ( $LocalFile.LastWriteTime -gt $CloundFile.LastWriteTime) { Write-Output "Local profile is newer, updating cloud" Copy-Item $LocalPSProfilePath -Destination $CloundFile -Force -Confirm:$false } |
Although I don’t put my functions directly in the profile. I have them in a separate module.
And since I never remember witch functions I have, I’ve put this in my profile.
1 2 3 4 |
Import-Module "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\PowerModule.psm1" function Get-PowerCommnds {Get-Command -Module PowerModule | SORT NAME} Set-alias GPC Get-PowerCommnds |
Hope you find this useful! 🙂
Keep on script’n
Andreas