UserProfile-File-Management

 

 Sometimes you have to export user files

 

Be it that their computer has gotten a virus or the profile is having issues so you cant migrate the profile in any convenient way, this script will help out in getting most of it!

Very useful when having to deal with many computers.

 


###########################################################################
## Export User Data From Old computer to new. #############################
###########################################################################
###########################################################################
## SETTINGS & SETTINGS & SETTINGS & SETTINGS & SETTINGS & SETTINGS & SET ##
###########################################################################

$ScriptDate         = "2012-05-24"                              # Date Modified
$Author             = "www.lennarkivistik.com"                  # Creator of this Awesome Script!
$Source             = "C:\Documents and Settings\$env:username" # Source Folder
$Destination        = "H:\$env:USERNAME"                        # Destination
$AppDataFolder      = "$env:AppData\Microsoft"                  # ApplicationData Folder
$Interactive        = $TRUE                                     # if set to false the script will have no user input. // Good for Automation eg. logonscripts
$End_Report         = $TRUE                                     # if you want a report when the export is finished.

### End Settings  #########################################################
###########################################################################
### Folder Arrays #########################################################

$AppDataItems       = @("UProof",       # Copy These Folders from AppData
                        "Proof",
                        "Signatures",
                        "Signaturer",
                        "Outlook")
                        
$PST_Paths          = @("Documents\Outlook Files",
                        "Local Settings\Application Data\Microsoft\Outlook",
                        "Lokala Inställningar\Application Data\Microsoft\Outlook")

$MyDocFolders       = @("My Documents",
                        "Documents",
                        "Mina Dokument")

$Desktop            = @("Desktop",
                        "Skrivbord")
                        
$Favorites          = @("Favorites",
                        "Favoriter")

$global:Report      =   ""               # Is empty but has to be declared

###########################################################################
## FUNCTIONS & FUNCTIONS & FUNCTIONS & FUNCTIONS & FUNCTIONS & FUNCTIONS ##
###########################################################################

function Make_Report ($Message)
{
    $Date = (GET-Date -format 'yyyy-MM-dd hh:mm:ss')
    $global:Report += " $Date - $Message`n"
}

function CountDown($Seconds, $Message)
{
    $DivideSecond  = 100/$Seconds
    $DivideSeconds = 0

    for($Second = 0; $Second -le $Seconds; ++$Second)
    {
        Write-Progress -Activity “Info” -PercentComplete $DivideSeconds -Status $Message; Sleep -Seconds 1;
        $DivideSeconds = $DivideSeconds + $DivideSecond;
    }
}

function CountFiles($Path)
{
    $files = (Get-ChildItem "$Path" -Recurse).count
    if($files -lt 1) {$files=0;}
    return $files
}

function Copy_Data($From, $List, $To, $Message, $Special)
{
    foreach ($Source_Folder in $List)
    {
        $FolderPath = "$From\$Source_Folder"
        
        if(Test-Path $FolderPath)
        {
            if($Interactive -eq $false){ $Special=$Special." /np /NFL /NDL /NJH /NJS"; }
            Write-Host $Special
            Robocopy "$FolderPath" "$To\$Source_Folder" $Special
            if(Test-Path "$To\$Source_Folder"){$Files = CountFiles "$To\$Source_Folder";}
            
            Make_Report "SUCCESS - Copied $Files files from - $FolderPath"
        }
        else
        {
            Make_Report " FAILED - Path does not exist - $FolderPath"
        }
    }
}

###########################################################################
## EXPORT DATA - EXPORT DATA - EXPORT DATA - EXPORT DATA - EXPORT DATA - ##
###########################################################################

function Export_AppData
{
        Copy_Data "$AppDataFolder" $AppDataItems "$Destination\AppData" "Done copying AppData files..." "/E"
}

function Export_MyDoc
{
        Copy_Data "$Source" $MyDocFolders "$Destination\Documents" "Done copying documents..." "/E"
}

function Export_Favorites
{
        Copy_Data "$Source" $Favorites "$Destination\Favorites" "Done copying favorites..." "/E"
}

function Export_Desktop
{
        Copy_Data "$Source" $Desktop "$Destination\Documents" "Done copying desktop..." "/E"
}

function Export_PST
{
        Copy_Data "$Source" $PST_Paths "$Destination\Outlook_PST" "Done copying PST files..." " *.pst /E"
}

function Export_All
{
    Export_AppData; Export_Favorites; Export_MyDoc; Export_Desktop; Export_PST;
}

###########################################################################
## IMPORT DATA - IMPORT DATA - IMPORT DATA - IMPORT DATA - IMPORT DATA - ##
###########################################################################

function Import_AppData
{
        Copy_Data "$Destination\AppData" $AppDataItems "$AppDataFolder\TEST\" "Done Importing AppData files..." "/E"
}

function Import_MyDoc
{
        Copy_Data "$Destination\Documents" $MyDocFolders "$Source\" "Done Importing My Documents" "/E"
}

function Import_Favorites
{
        Copy_Data "$Destination" $Favorites "$Source\" "Done Importing Favorites" "/E"
}

function Import_Desktop
{
        Copy_Data "$Destination\Documents" $Desktop "$Source\" "/E"
}

function Import_PST 
{
        if(Test-Path "$Destination\Outlook_PST")
        {
           # Robocopy "$Destination\Outlook_PST" "$Source\Documents\Outlook Files"  "*.pst /E"
        }
}

###########################################################################
## END FUNCTIONS END FUNCTIONS END FUNCTIONS END FUNCTIONS END FUNCTIONS ##
###########################################################################


Write-Host "
###############################################################
#`n#`t`t`t **** WINDOWS PROFILE MIGRATION ****
#`n# `t Userdata Migration & Preparation Script for users.
#`t $Author Script updated $ScriptDate
#`t This script will copy files from your computer to $Destination
#
###############################################################"

if($Interactive -eq $TRUE)
{
    $Export_Import = Read-Host("Do you want to export user_data(1) or import user_data(2)")
    
    if($Export_Import -eq 1)
    {

        $Automatic_Manual = Read-Host("Do you want to copy automaticaly(1) or choose what to copy(2)?")
    
        if($Automatic_Manual -eq 1)
        {
            Export_All
        }
        elseif($Automatic_Manual -eq 2)
        {
            $UserAppData   = Read-Host("Do you want to export user AppData(y) else (n)?")
            $UserFav       = Read-Host("Do you want to export user Favorites(y) else (n)?")
            $UserDocuments = Read-Host("Do you want to export user My Documents(y) else (n)?")
            $UserDesktop   = Read-Host("Do you want to export user Desktop(y) else (n)?")
            $UserPST       = Read-Host("Do you want to export user PSTFiles(y) else (n)?")
        
            if($UserAppData   -eq "y") { Export_AppData   }
            if($UserFav       -eq "y") { Export_Favorites }
            if($UserDocuments -eq "y") { Export_MyDoc     }
            if($UserDesktop   -eq "y") { Export_Desktop   }
            if($UserPST       -eq "y") { Export_PST       }
        }

        if($End_Report -eq $TRUE)
        {
            #clear
            Write-Host "`n`n` END REPORT - Destination Path: $Destination"
            Write-Host " --------------------------------------------------------------------------"
            Write-Host "$global:Report"
        }
    }
    elseif($Export_Import -eq 2)
    {
        Import_AppData
        Import_Favorites
        Import_MyDoc
        Import_Desktop
        Import_PST
    }
}
else
{
    Export_All
}

Posted on: Wednesday 07 January 2015