[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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.
0
Dead_Eyes
Asked:
Dead_Eyes
  • 6
  • 3
1 Solution
 
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
 
QlemoC++ DeveloperCommented:
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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
 
QlemoC++ DeveloperCommented:
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
 
Dead_EyesAuthor Commented:
Thanks worked perfectly and great explanation. I will be learning from this script for while.
0
 
QlemoC++ DeveloperCommented:
Great it worked for you. Don't hesitate to ask for details about above script.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now