Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Move Users Favorites Folder and then Delete?

Posted on 2014-03-03
Medium Priority
Last Modified: 2014-03-05

My current scenario is as follows for user accounts:
\\\userprofile$\User-16 (this is their profile path)
\\\userprofile$\User-16\Favorites (this is their favorites folder)

I need to take the above favorites folder and move it to their userhome$ folder found here:

\\\userhome$\%username%\Favorites (these folders already exist)

Then delete their complete userprofile$ folder.

How can I achieve this using powershell? I think the tricky part for me is going through each folder in userprofile$ and looking for a \Favorites folder and then matching that userprofile$ foldername to %username% since they're the same.
Thank you.
Question by:levertm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 29

Expert Comment

ID: 39901194
if (Test-Path -Path "\\\userprofile$\User-16\Favorites") 
move-item -path  \\\userprofile$\User-16\Favorites -destination \\\userhome$\%username%\
remove-item -path  \\\userprofile$\User-16 -recurse

Open in new window

Something like above should work, very simple.

The if statement verifies if the folder exists.

If it does you proceed to move the favorites folder

Then delete the userprofile folder

Author Comment

ID: 39901231
Hi becraig,

Thanks for the input, it's a good start. How do I go about talking over 100 user accounts? I would have to run this script with variable so as to not set any hard coded values for account usernames (ie: user-16)
LVL 29

Accepted Solution

becraig earned 2000 total points
ID: 39901249
gc users.txt | % {
$user = "$_"
if (Test-Path -Path "\\\userprofile$\$user\Favorites") 
move-item -path  \\\userprofile$\$user\Favorites -destination \\\userhome$\$user\
remove-item -path  \\\userprofile$\$user -recurse

Open in new window

This is assuming you have a text file with the username one per line (Matching the exact folder names)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39901256
I will generate a text file and test your code in my environment right now!

Be back later with my findings and results.

Author Comment

ID: 39901366
Thank you becraig, that worked. Although I had to use the copy-item instead as the move-item gave me errors.
LVL 29

Expert Comment

ID: 39901382
ok sounds good.

As a rule of thumb I always copy (just in case)

Author Comment

ID: 39907786
I also posted this http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28381231.html begraig if you're interested to take a stab :)

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question