copying into users appdata.. or other user folders..

Topics: Archive - General
Nov 19, 2013 at 1:13 PM
c'et possible?

Is it possible to interact in any way with the user folders?
Apart from hardcoding of course.. I can't really do copy-file -filepath "C:\users\blabla\appdata"

Right now I need to copy a file into all users appdata folder.. but perhaps only solution is to create a package next to my application that copies the file for each user that logs in..

Any other ways?
Coordinator
Nov 19, 2013 at 1:31 PM
Edited Nov 19, 2013 at 1:32 PM
You can only interact with the current user if you're running as the current user. If you want to copy a file to each individual user profile, here's a quick and dirty way to do it, using the Toolkit Copy-File function:
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
    Copy-File -Path "$dirFiles\MyFile.txt" -Destination "C:\Users\$user\AppData\Local\MyApp\MyFile.txt"
}
This will check C:\Users for each folder (except Public) and copy your file to the relevant folder. There's probably a cleaner way to do it but this should work.

Cheers, Dan
Apr 16, 2014 at 1:33 PM
Dan, I know this is an older post but it's relevant to a similar issue I'm having; not only do I have to copy a file, but I also have to create a directory in "C:\Users\%userprofile%\Appdata\Local\" two levels deep, then copy the ini file to the bottom most folder. Using the above, could I do something similar to create the folder structure first, then apply the ini file?

For whatever reason, it's failing to create the directory and of course, isn't copying the file because the directory doesn't exist. Is my code incorrect or...
As reference, I'm using SCCM to deploy this Application, if that matters.
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
    New-Item -ItemType Directory -Force -Path "C:\Users\$user\AppData\Local\Folder1\Folder2\"
}

ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
    Copy-File -Path "$dirFiles\MyFile.txt" -Destination "C:\Users\$user\AppData\Local\Folder1\Folder2\MyFile.ini"
}
Coordinator
Apr 16, 2014 at 1:43 PM
Edited Apr 16, 2014 at 1:44 PM
Woops. Correct code should be:
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
    New-Item -ItemType Directory -Force -Path "C:\Users\$($user.Name)\AppData\Local\Folder1\Folder2\"
    Copy-File -Path "$dirFiles\MyFile.txt" -Destination "C:\Users\$($user.Name)\AppData\Local\Folder1\Folder2\MyFile.ini"
}
Hope this helps.

Dan
Apr 16, 2014 at 1:52 PM
running it as a package gives you the option to do it every time a user logs in.. if you need to consider future users that haven't been logged onto the computer prior to you deploying the file.
Apr 21, 2014 at 6:26 PM
Dan, sorry for the late reply but I wanted to get back and let you know this worked perfectly. Thank you.
Now, I just have to figure out how to detect the user's location to make sure they get the correct INI file. :)

Thanks again. This saved us a huge amount of time.