Skip to content

Instantly share code, notes, and snippets.

@NathanTheGr8
Created August 16, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanTheGr8/fc35d1d309f179da0831ea0ad738b7b5 to your computer and use it in GitHub Desktop.
Save NathanTheGr8/fc35d1d309f179da0831ea0ad738b7b5 to your computer and use it in GitHub Desktop.
$OneDriveFolder = "$home\OneDrive\Documents\.backup"
$Date = Get-Date -Format "MM-dd-yyyy"
$logFile = "$OneDriveFolder\Backup-UserFoldersLog_$Date.log"
Start-Transcript -Path "$logFile" -Append -Force
if(!(Test-Path -Path $OneDriveFolder)){
New-Item -Path $OneDriveFolder -ItemType Directory
}
if (!((get-item $OneDriveFolder -Force).Attributes.HasFlag([System.IO.FileAttributes]::Hidden))) {
Write-Output "Hiding backup folder $OneDriveFolder"
(Get-Item $OneDriveFolder -Force).Attributes += 'Hidden'
}
else {
Write-Output "Backup folder $OneDriveFolder already hidden"
}
$foldersToBackup = @{
"Favorites" = "$home\Favorites"
"ChromeBookmarks" = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks*"
"ChromeBookmarksBackup" = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks.bak"
"EdgeFavorites" = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\Favorites"
}
foreach ($folder in $foldersToBackup.Keys){
$Source = "$($foldersToBackup.Item($folder))"
$Destination = "$OneDriveFolder\$($folder)"
if (Test-Path -Path "$Source"){
if (!(Test-Path $Destination -PathType Container)){
Write-Output "Directory $Destination didn't exist. Creating directory."
if (Test-Path $Destination -PathType Leaf) {
Write-Output "Deleting file $Destination before creating a folder of the same name."
Remove-Item $Destination -Force -Verbose
}
New-Item -Path $Destination -ItemType Directory -Verbose -Force
}
Write-Output "Copying $($folder) from $Source to $Destination"
#Splatting the Parms so I can conditionally add more
#https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6
$HashArguments = @{
Path = $Source
Destination = $Destination
Verbose = $true
Recurse = $true
Force = $true
}
#Change path Parameter if the Soruce is a folder so we copy the folder contents and not the folder and the contents
if (Test-Path $Source -PathType Container) {
$HashArguments["Path"] = "$Source\*"
}
Copy-Item @HashArguments
}
else {
Write-Output "$Source didn't exist, not backuping up."
}
}
Stop-Transcript
$OneDriveFolder = "$Home\OneDrive\Documents\.backup"
$Date = Get-Date -Format "MM-dd-yyyy"
$logFile = "$OneDriveFolder\Restore-UserFoldersLog_$Date.log"
Start-Transcript -Path "$logFile" -Append -Force
$foldersToBackup = @{
"Favorites" = "$home\Favorites"
"ChromeBookmarks" = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks"
"ChromeBookmarksBackup" = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks.bak"
"EdgeFavorites" = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\Favorites"
}
foreach ($folder in $foldersToBackup.Keys){
$Source = "$OneDriveFolder\$($folder)"
$Destination = "$($foldersToBackup.Item($folder))"
if (Test-Path -Path $Source){
Write-Output "Copying $($folder) from $Source\* to $Destination)"
Copy-Item -Path "$Source\*" -Destination $Destination -Verbose -Recurse -Force
}
else {
Write-Output "Backup folder $Source didn't exist, nothing to restore."
}
}
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment