how to loop through all user directories on file server??

by tommls at 2013-02-22 10:06:05

All our users have directories in //fp/users, where each directory is //fp1/users/%username%/
I know how to test if they have a Favorites directory, if true cd/move to that folder, create folder ‘Saved’, copy all contents of Favorites to Saved
What I don’t understand is how to loop through the directories, how do I do this??

<air code>
for each directory (%username%) in //fp1/users
if Favorites folder exists
cd to Favorites I know how to do this
create new folder 'Saved" I know how to do this
copy all contents to Saved I think I can figure this out
must I now CD back up to ///fp1/users after doing the copy??
else continue?? (no Favorites folder exists)
</air code>

or should it be if not Favorites exist continue otherwise assume it exists and CD to it etc.??
What I don’t understand is how to go through directories where the %username% is different…

Thank you, Tom
by tommls at 2013-02-22 10:42:39
this starts toward what I want:

#Get User Folder names into variable for ForEach Loop
$UserFolders = get-childitem -path “\fp1\users” | where-object {$.Psiscontainer -eq “True”} |select-object name
#Loop through folders in Directory
foreach ($UserFolder in $UserFolders)
If(-Not(Test-Path “\$UserFolder\Favorites”)) { # if true exit for loop, go to next folder
Set-Location “\$UserFolder\Favorites” -ErrorAction SilentlyContinue # if exists move to Favorites
if (-not (Test-Path “Saved”)) {
New-Item -Path “Saved” -ItemType “Directory” -ErrorAction SilentlyContinue
copy \$UserFolder\Favorites*.* \$UserFolder\Favorites\Saved

will this work??
thank you, tom
by tommls at 2013-02-22 11:13:27
Result looks right, I can sort of figure out how to do the reverse to restore favorites.

Having little experience with what is or is not a correct -Whatif output, this APPEARS to appropriately create the Saved directory and copy over the files…
What if: Performing operation “Copy Directory” on Target “Item: \fp1\users\vnephew\Favorites Destination: \fp1\users\vnephew\Favorites\Saved”.

However it does not work if the Saved folder already exists, how do I fix this??
I’ll need to run the script multiple times…
That is why I wrote the script the way I did, I had other code which tests for directory existence…

Thank you…
by poshoholic at 2013-02-22 11:24:09
I have a recommendation. You are copying the contents of a folder into a subfolder that is under that folder. Is the location of the subfolder (under the folder you are copying) an absolute requirement? Because if not I would recommend you copy it to a sibling folder instead. For example, copy Favorites to SavedFavorites as a sibling in the user profile folder. That makes the script much simpler (the recursive copy into a subfolder is a little more complicated due to limits/bugs in PowerShell). If using a sibling folder instead is fine, then you could do this:
Get-ChildItem -Path \fp1\users*\Favorites | Copy-Item -Force -Destination {"$($
.PSParentPath)\SavedFavorites"} -Recurse -WhatIf
Similarly, copying the saved favorites back would be this:
Get-ChildItem -Path \fp1\users*\SavedFavorites | Copy-Item -Force -Destination {"$($.PSParentPath)\Favorites"} -Recurse -WhatIf
And that removes the mistake I made which would only work when the script was run the first time (this works every time).
by tommls at 2013-02-22 11:34:23
In testing in the first run, the above code created a subfolder, SavedFavorites, within Favorites.
Then on the 2nd run, I got this:
Copy-Item : Cannot copy item \fp1\users\vnephew\Favorites\SavedFavorites onto itself.
Plus I discovered some people have a ‘Favorites Bar’ FOLDER inside ‘Favorites,’ which also must be copied to SavedFavorites if it exists.
Thank you, Tom
by poshoholic at 2013-02-22 12:38:02
Ok, I’m still scratching my head on why the last one is doing that. It doesn’t make sense, but it’s Friday, and it’s been a long week, so I’ll have to leave it as not making sense to me for now.

This one should work better:
Get-Item -Path \fp1\users*\Favorites | ForEach-Object {
$parentPath = $
$_ | Get-ChildItem -Force | Copy-Item -Force -Destination “${parentPath}\SavedFavorites” -Recurse -WhatIf

That should copy over both files and folders, even if they are hidden.
by tommls at 2013-02-22 12:40:49
Thank you for your new suggestion…
Testing the new script displays proper What-If results but when the What-If is removed, nothing happens!! :frowning:
No error messages either, I will keep working on this and also try/test my own trial script and try to remember to update this thread…
Thank you for helping me, Tom
by poshoholic at 2013-02-22 13:02:56
Wait, so when -WhatIf is removed, this version doesn’t copy the Favorites folder and its contents as a SavedFavorites sibling folder (at the same level, right next to it)? This works fine when I run it locally. It doesn’t output anything, but it works.
by tommls at 2013-02-22 13:15:39
Pllease remember that since today is Friday, ghosts are in the machines. :slight_smile:

After RDP’ing in again and trying again, I do get a resulting sib folder…the script does work. :slight_smile:

The Favorites Bar folder within the user’s Favorites folder was saved within SavedFavorites as Links.
Do you know why that happens??
If you don’t I’m not gonna worry bout it :slight_smile:

Thank you, Tom
by poshoholic at 2013-02-22 13:22:25
I thought the favorites bar folder was actually called Links, and that “Favorites Bar” was just the name according to Internet Explorer or other web browsers.
by tommls at 2013-02-22 13:24:59
OIC makes sense, thank you for explaining…
Thank you, Tom