Scripting file replacement across local profiles

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 11 months ago.

  • Author
    Posts
  • #15167
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    Hello,

    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}
        }
    }
    
    
    
    
    
  • #15168
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    }
    
  • #15170
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    Dave,

    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."

  • #15171
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

You must be logged in to reply to this topic.