Scripting file replacement across local profiles


I’ve run into a snag and I hope someone can assist. Here’s the short version…

  1. Grab all users on a workstation
  2. For each user, check for folder path
  3. If the path exists, check for multiple folders in that path and replace a file within each one

Easy enough right? Not so much… here is what i have so far


<# Variables #>
$users= Get-Childitem "c:\users" | Select-Object -Property name 
$profiles=Get-ChildItem "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles" | select-object name
$ffox = "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*\secmod.db"

Foreach ($user in $users)

    $profiles=Get-ChildItem "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles" | select-object name
            Foreach ($profile in $profiles){
                if ((Test-Path $ffox) -eq $true)
                    {Copy-item "\\networkshare.fq.dn\c$\directory\file.txt" -Destination "\AppData\Roaming\Mozilla\Firefox\Profiles\$ffprofile" -Force 
                         write-host $user $ffprofile}

There are a couple of things that jump out at me as being off. Your Test-Path command is working against a wildcard instead of the $profile variable of your foreach loop, and your Copy-Item command is using $ffprofile instead $profile. That said, Get-ChildItem has some nice behavior with wildcard paths that can simplify your code quite a bit. Try this:

Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\secmod.db' |
ForEach-Object {
    Copy-Item -Path '\\networkshare.fq.dn\c$\directory\file.txt' -Destination $_.DirectoryName


I appreciate all of the help. Who knew that posting to my two favorite forums, i would get help on both from the same person!

My test path command was there because that DB file is the one I need to write over. Really i could lose that all together. If I can keep any folder within \Profiles as an object and just copy that file into each one of those objects. I haven’t had to work with multiple foreach statements yet so I think i’m “tying my shoelaces together.”

If you aren’t concerned with that secmod.db file, then you can leave that part out. This would work:

Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*' -Directory |
ForEach-Object {
    Copy-Item -Path '\\networkshare.fq.dn\c$\directory\file.txt' -Destination $_.FullName

(The -Directory switch to Get-ChildItem requires at least PowerShell 3.0. If you’re running 2.0, you can take that out and use Where-Object { $_.PSISContainer } instead.