Powershell moving files

Hi I am relatively new to PowerShell but am sure it is the solution to an interesting problem I am facing. I have been tasked with helping to clear up the file structure of users Documents areas in a school and after talking with staff they would like all current files (DocX, XLSX, JPG, PNG, TXT & PPTX) to be moved to a folder called archive to be located in there Documents folder. The folder structure on the file server is layed out as follows
D:\Users\Staff\Teachers\%Username%\Documents
D:\Users\Staff\TAs\%Username%\Documents
D:\Users\Staff\Office\%Username%\Documents
D:\Users\Students\YOE2010\%Username%\Documents
D:\Users\Students\YOE2011\%Username%\Documents
D:\Users\Students\YOE2012\%Username%\Documents
D:\Users\Students\YOE2013\%Username%\Documents

So first job is creating a script that can make a folder called archive in each users Documents folder
And the second task is to create a script that will scan each Documents folder (Not subfolders like my pictures ect) and move certain file types to the newly created archive folder

There a few challenges to this but I believe its more than possible to do in a single PowerShell script. I will post what script I can but would really appreciate some expert input and if its possible can you post any script as verbose as possible as I am trying to learn as I go.
Dead_EyesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dead_EyesAuthor Commented:
Ok I have got this far but it all going wrong somewhere
Get-ChildItem -Path D:\Users\ -Filter "Documents" -Recurse -Force |
 Where-Object {$_.PSIsContainer}|
  ForEach-Object {$_.FullName}| Out-file D:\Users\Temp.txt
Get-Content D:\Users\Temp.txt | ForEach-Object {New-Item -Name Archive -ItemType Directory}
0
Dead_EyesAuthor Commented:
Ok realised my mistake soon enough and looks like step one works (open to critical comments though):
Get-ChildItem -Path D:\Users\ -Filter "Documents" -Recurse -Force |
 Where-Object {$_.PSIsContainer}|
  ForEach-Object {$_.FullName}| Out-file D:\Users\Temp.txt
Get-Content D:\Users\Temp.txt |
 ForEach-Object {New-Item -Path $_ -Name Archive -ItemType Directory}
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That's indeed a good start, but you try to avoid creating intermediate variables or files. I also like to use some very common abbreviations for cmdlets, in particular ? for Where-Object and % for Foreach-Object.
Get-ChildItem -Path D:\Users\ -Filter "Documents" -Recurse -Force |
  ? { $_.PSIsContainer } |
  % {
    New-Item -Path $_ -Name Archive -ItemType Directory -ErrorAction SilentlyContinue
    Get-ChildItem $_ | ? { ! $_.PsIsContainer } | Move-Item -Destination "$($_.Directory)\Archive"
  }

Open in new window

Note that I added the -ErrorAction (short -EA) to allow execution without errors even if the folder exists already.
I intentionally left out an extension filter, as you said "all files".
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

Dead_EyesAuthor Commented:
Hi thanks for the answer and the explanation. Would it be possible for you to show me how to add a filter while I test run the script?
0
Dead_EyesAuthor Commented:
Hi I just tried running the script and it did not throw any errors but it appears to have done nothing?
0
Dead_EyesAuthor Commented:
Hi I have tired altering a few things but no luck, have to head out now but if you find the problem and can fix I will award full points as soon as I get back
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry for that - several bugs in that simple script :/
Get-ChildItbem -Path D:\Users\ -Filter "Documents" -Recurse -Force -ea SilentlyContinue |
  ? { $_.PSIsContainer } |
  % {
    New-Item -Path $_.FullName -Name Archive -ItemType Directory -ea SilentlyContinue -whatif
    Get-ChildItem $_.FullName |
      ? { ! $_.PsIsContainer -and '.pdf', '.doc', '.txt' -contains $_.Extension } |
      Move-Item -Destination "$($_.FullName)\Archive" -whatif
  }

Open in new window

The -whatif switch is great for testing - it displays what the cmdlet would do, but does not perform the action. If you are satisfied with the paths and actions, remove that switch.
I've included three extensions as an example. Shouldn't be difficult for you to add more.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dead_EyesAuthor Commented:
Thanks worked perfectly and great explanation. I will be learning from this script for while.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Great it worked for you. Don't hesitate to ask for details about above script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.