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.
Default
notdefaultuser0
which is something else. Try putting it inC:\Users\Default\AppData\Roaming
. This should work for new accounts as @music2myear mentioned earlier and your script should work for existing.