Power Shell Script Issue - Backing up User Profiles to Shares RRS feed

  • Question

  • #
    # User Profile Backup
    # Users Profile Location
    $userprofile = dir -Path 'C:\Users' -Exclude 'Default*','All Users','Admin*', 'Public', 'Station', '*la', '*da' | Select-Object -Expand FullName
    # Split at C:\Users -> \ username
    $sharename = $userprofile.split("\")[-1]                     # Split to first folder name
    # Shared Drive Locations
    $HD01 = "\\yourserver\$sharename\Backup"  # Home Drive Location
    $path1 = test-Path $HD01                                     # Test Directory
    # Only Select Included Folders
    $IncludedFolders = 'Desktop\*', 'Favorites\*', 'Documents\*', 'AppData\Local\Microsoft\OneNote\15.0\Backup\*', 'AppData\Roaming\Microsoft\Sticky Notes\*', 'AppData\Roaming\Microsoft\Outlook\*.pst', 'AppData\Local\Microsoft\Windows\Themes\*.theme'
    $Stuff = dir $userprofile -Recurse -Include $IncludedFolders
    # Backup to Home Drives
    if ($path1 -eq $true)
           # Get a list of programs installed
           gwmi -Class win32_product | Select Name | out-File "$HD01\programs_list.txt"
           # Get Computer Name
           Get-Childitem env:computername | Select Value | out-File "$HD01\computer_name.txt"
           # Copy files to HomeDrive
           copy-Item $Stuff -Force -Destination "$HD01\Backup"
           $backup_log = Dir -Recurse $HD01 | out-File "$HD01\backup_log.txt"

    Hello everyone,

    I'm newer to powershell and Systems Administration (I'm a JR Sys Admin), I came from desktop and one of the issues we ran into was backing up user profiles manually over and over again. So instead of using the windows tool to back them up I wanted to make a powershell script the guys could literally just run off of any PC. My issues is $Stuff isn't returning a value any longer if I use the -Include switch. But the issue without using the include switch is the data is completely jumbled together that I'm copying to the share.. It stays structured if I use the include which worked up until I added the AppData information to it as well. Even after removing the AppData includes it still doesn't work any longer.

    Pleas advise.


    • Edited by Antillius Tuesday, August 23, 2016 9:01 PM
    • Moved by Bill_Stewart Tuesday, October 11, 2016 6:37 PM This is not "fix/debug/rewrite/add feature(s) to this script I found on the Internet" forum
    Tuesday, August 23, 2016 5:42 PM

All replies

  • You will have issues with this.   Use RoboCopy as it is faster and copies all file attributes.

    The best way t keep profiles backed up is to redirect all folders to a files server then roam the profile.  Just schedule a task backup to backup all user folders as often as needed.

    Why reinvent the wheel or try to redesign Windows. Use it as it was designed to be used and you will save a lot of work and problems.


    Tuesday, August 23, 2016 6:35 PM