1

I want to create a script that copies a file to all user profiles both who have already logged and for a new user who log into the windows 10 pc. The location the files should be copied to is Users\username\AppData\Roaming

Please help not great at powershell

Here is my current script. It will copy to existing profiles but I need to add line to copy for new users who login each time

$Source = '\\FileShare\FancyConfigurationFiles\Config.xml' 
$Destination = 'C:\users*\AppData\Roaming\' 
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force}
  • 1
    Why do you need PowerShell for copying one file? – harrymc Feb 7 '19 at 16:08
  • After deployment of an application I need to push this file to multiple computers and different users login to these machines so I need each profile to get the file once a user has logged on. – Average Joe Feb 7 '19 at 16:11
  • 1
    Place the files in the Default User profile and they will be included in every new profile that is created as new people log in. – music2myear Feb 7 '19 at 16:20
  • I tried putting the file in the defaultuser0 profile and it didn't put the file in any existing profiles music2myyear let alone a newly logged in profile. Any other suggestions? – Average Joe Feb 7 '19 at 16:37
  • 1
    You said "I tried putting the file in the defaultuser0". This is not correct - it should be Default not defaultuser0 which is something else. Try putting it in C:\Users\Default\AppData\Roaming. This should work for new accounts as @music2myear mentioned earlier and your script should work for existing. – lx07 Feb 7 '19 at 18:57
0

C:\Users\Default\ contains files and settings used to create profile folders for any accounts logging in to a computer that do not yet have a profile folder in C:\Users\

If you wish for files to be included in the profiles for any new logins, place them in the desired location within this directory structure.

For accounts already logged into the computer that have profile folders, placing files in .\Default\ will not make any change. For these you can either manually or by script place the files into the desired folder, or you can delete the local profile folder (which results in the loss of any data saved locally) which means the profile folder will be created from the .\Default\ folder on the next login.

|improve this answer|||||
0

I would start by asking the registry where the profiles are because they, theoretically, can be customized. In that regard, so can the location of the AppData\Roaming folder in each profile, so we should account for that as well to be thorough.

# Get the list of profile paths from the registry; since they theoretically can be customized.
$profileListReg = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'

# This filters out short SIDs (such as the system account)
$profilesReg = Get-ChildItem $profileListReg | Where-Object { $_.Name.Split('-').Count -gt 4 }

$userProfiles = @{}

# Add Default Profile to hashtable
foreach ($profileReg in $profilesReg) {
    $userProfiles.Add($profilesReg.PSChildName, (Get-ItemProperty ('Registry::{0}' -f $profileReg.Name)).ProfileImagePath)
}

# Add Default Profile to hashtable
#   This will cover new users getting the file
$userProfiles.Add('.DEFAULT', (Get-ItemProperty $profileListReg).Default)

$source = '\\FileShare\FancyConfigurationFiles\Config.xml'
$userShellFoldersReg = 'Registry::HKEY_USERS\{0}\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'

# Copy Config to each Profile ...
foreach ($userProfile in $userProfiles.GetEnumerator()) {
    Write-Verbose "$($userProfile.Name): $($userProfile.Value)"

    $userShellFolders = Get-Item ($userShellFoldersReg -f $userProfile.Name)
    $appData = $userShellFolders.GetValue('AppData','','DoNotExpandEnvironmentNames')
    Write-Verbose "AppData: ${appData}"

    $destination = $appData.Replace('%USERPROFILE%', $userProfile.Value)
    Write-Verbose "Destination: ${destination}"

    Copy-Item -Path $Source -Destination ($destination -f $userProfile) -Force
}

You should comment out the Copy-Item line at the bottom and turn on verbosity ($VerbosePreference = 'continue') to test and ensure those verbose messages look like what you expect them to.

Note: I do not like using HKLM: because interacting with the registry doesn't return full paths with HKLM:, it returns HKEY_LOCAL_MACHINE. So, you have to either string replace those or know that you can just tack Registry:: on the front of it and get to the path. This is more useful anyway since all hives are not available as PS Drives.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.