简体   繁体   English

PowerShell 将多个文件夹的子项移动到它们自己的子文件夹中

[英]PowerShell to move subitems of multiple folders into their own subfolders

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.想寻求一些有关 PS 脚本的帮助,该脚本会将用户文件夹中的文件移动到新的“文档”子文件夹中,因为我们面临着为许多配置文件手动执行此操作。

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

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PowerShell在多个父文件夹中创建子文件夹 - Creating subfolders with PowerShell in multiple parent folders powershell-在多个父文件夹中批量创建子文件夹 - powershell - bulk create of subfolders in multiple parent folders Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中 - Powershell: Move all files from folders and subfolders into single folder 使用powershell根据文件名移动/创建文件夹/子文件夹 - Using powershell to move/make folders/subfolders based on filename 使用不同的父文件夹 CSV 创建多个子文件夹 - Powershell - Create multiple subfolders with different parent folders CSV - Powershell PowerShell:查找子文件夹中没有文件的文件夹 - PowerShell: Finding folders with no file in subfolders PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置 - PowerShell script to move files and folders including subfolders from one location to another older than x days Powershell - 移动按子文件夹过滤的文件夹 - Powershell - Move folder filtered on subfolders 使用Powershell移动文件夹和内容 - Move folders and contents with Powershell 将 pdf 文件从子文件夹移动到 1 个主文件夹,并为多个文件夹重复使用脚本 - Move pdf files from subfolders to 1 main folder and re-use script for multiple folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM