Search code examples
powershellxenapp

Powershell: Script to search all user profiles and copy the most recent to all user profiles


I am looking to write a powershell script to search all user profiles on a server for a specific file, compare the files by the lastmodifieddate, and then copy the newest file to all user profiles. The script will also create a backup of the last three versions of the file.

I previously wrote this script for our pilot environment where only two people were accessing the app (this is for a XenApp), but the user base has now expanded and I would like to create the prod version of the script to cover future growth.

Any help would be very much appreciated. Thanks! Script below...

$SRC1 = "\\Server\c$\Users\XXXX1\AppData\Roaming\EMIESiteListManager\sitelist.xml"
$SRC2 = "\\Server\c$\Users\XXXX2\AppData\Roaming\EMIESiteListManager\sitelist.xml"
$SRC3 = "\\Server\c$\Users\XXXX3\AppData\Roaming\EMIESiteListManager\sitelist.xml"
$BKU = "\\storage\IT\EMSLM\Backup"

if ( (get-item $SRC1).LastWriteTime -gt (get-item $SRC2).LastWriteTime )  {Copy-Item $SRC1 $SRC2}
    else {Copy-Item $SRC2 $SRC1}

if ( (get-item $SRC1).LastWriteTime -gt (get-item $SRC3).LastWriteTime )  {Copy-Item $SRC1 $SRC3}
    else {Copy-Item $SRC3 $SRC1}

if ( (get-item $SRC1).LastWriteTime -gt (get-item $SRC2).LastWriteTime )  {Copy-Item $SRC1 $SRC2}


Remove-Item $BKU\sitelist_old_2.xml
Rename-Item $BKU\sitelist_old_1.xml $BKU\sitelist_old_2.xml
Rename-Item $BKU\sitelist.xml $BKU\sitelist_old_1.xml
Copy-Item $SRC1 $BKU

& 'C:\Program Files (x86)\Enterprise Mode Site List Manager\EMIESiteListManager.exe'

Exit

Solution

  • this isn't everything, but it should be a good place to start

    $users = dir "\\Server\c$\Users" -Directory | select -ExpandProperty fullname
    $newest = dir "\\Server\c$\Users\*\AppData\Roaming\EMIESiteListManager\sitelist.xml" | sort lastwritetime -Descending | select -First 1 -ExpandProperty fullname
    
    $files = @()
    
    $users | % {
        $files += $newest -replace [regex]::Escape($_)
    }
    
    $newestEnd = $files | sort {$_.length} | select -f 1
    
    $users | % {
        $dest = Join-Path $_ $newestEnd
        copy $newest $dest -force
    }