Share via


Scripting file replacement across local profiles

Question

Friday, May 9, 2014 7:19 PM

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}
    }
}

All replies (7)

Friday, May 9, 2014 7:35 PM

The array elements in $PROFILES are objects and not text strings.  You will specify the object property name to get the directory name as a text string.  Get rid of the "select-object name" as it's not needed.

I'm not sure where you are getting $FFPROFILE from, is that supposed to be $PROFILE?  You would specify the object property name as $($PROFILE.Name) within quotes to use it as text string, rather than using $PROFILE (the object).

 {Copy-item "\networkshare.fq.dn\c$\directory\file.txt" -Destination "\AppData\Roaming\Mozilla\Firefox\Profiles\($profile.name)" -Force


Friday, May 9, 2014 7:43 PM | 1 vote

This was also posted over on PowerShell.org. Here's what I suggested there:

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
}

Friday, May 9, 2014 7:46 PM

You're correct, $FFProfile is now $Profiles.  Troubleshooting typo :)  I wasn't getting any sort of error output or anything so i was looking for some indication that something was happening.  I'm not sure if i have explained the flow properly.

1. I need to check for all of the local user accounts with %USER%\Appdata\Roaming\Mozilla\Firefox

                   For example....c:\users\Admin\appdata\roaming\Mozilla\Firefox

                                           c:\users\Poweruser\appdata\roaming\mozilla\firefox

2. I need to store each user as an object in an array (or a better way?)

3. for each user, i need to drill further down into the firefox folder to \profiles

4. There may or may not be more than one here.  Once i get to the profiles folder, i just need to replace a file within.

5. This file needs to be copied into that directory for each local user.

Does that make any more sense?

Paul


Friday, May 9, 2014 7:58 PM

Paul,

1. You shouldn't declare the variables outside the loop, they will never be updated outside the loops. $profiles and $ffox will never contain the appropriate user.

2. Also in your second loop you use $profile instead of $ffprofile.

3. The Path you are using for $ffox is not valid. * is not a valid folder name. I'm not sure what you are checking for but I assume that you are looking for $ffprofile is valid.

Try this instead:

# Variables
$users= Get-Childitem "c:\users" | Select-Object -Property name 

Foreach ($user in $users) {
    
    #Verify The profiles Directory Exists
    $path = test-path "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles"
    
    # If $path = True (Implied True in the below statement)
    if ($path) {
       
        $profiles=Get-ChildItem "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles" | select-object name

         Foreach ($ffprofile in $profiles){
            ################ Correct This Path ###############
            $ffox = "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\$ffprofile\secmod.db"

            #Test the Path
            $path = Test-Path $ffox 
        
            # If $path = True (Implied True in the below statement)
            if ($path) {
                Copy-item "\\networkshare.fq.dn\c$\directory\file.txt" -Destination "c:\users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\$ffprofile" -Force 
                write-host "User: $user"
                Write-host "Profile: $ffprofile"
            }
        }
    }
}

 

P.S. I didn't test the code -- but it should be close.

I don't know if "name" is a valid object property for $profiles.

-Brenton

 

 


Friday, May 9, 2014 8:01 PM

3. The Path you are using for $ffox is not valid. * is not a valid folder name. I'm not sure what you are checking for but I assume that you are looking for $ffprofile is valid.

Just a note here - you can use wildcards in PowerShell paths. Example:

Get-ChildItem C:\Users\\Desktop

Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)


Friday, May 9, 2014 8:47 PM

Cool! I learn something new every day :)

I am not sure how PowerShell would handle that command in the instance where it has multiple results. (probably default to the first result?) -- something for a side thread I suppose.


Monday, April 16, 2018 6:23 PM

This helped with my issue tremendously, I've been searching for months and this is the solution I was looking for.

Thank you!