1

I'm working on a project cleaning up a file server's folder redirection folders. Would like to ask for some help with a PS script that would move the files in user folders into new "Documents" sub-folders, as we're facing manually performing this for a lot of profiles.

A tree example currently looks like:

―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx

I'd like to be able to achieve:

―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx

1 Answer 1

2

This should work, bear in mind for future questions you should provide at least a minimal attempt at solving the problem.

The inline comments should help you with the code logic.

# get all folders in `FolderRedirection`
foreach($dir in Get-ChildItem .\FolderRedirection -Directory) {
    # create a new `Documents` folder in each sub folder
    $dest = $dir | Join-Path -ChildPath Documents
    $dest = New-Item $dest -ItemType Directory -Force
    # get all files in each folder and move them to the new folder
    $dir | Get-ChildItem -File | Move-Item -Destination $dest
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.