在多个父文件夹中使用PowerShell创建子文件夹

时间:2014-09-22 18:38:41

标签: powershell

我希望在网络共享上的几个不同的父文件夹中创建子文件夹。我有大约900个客户文件夹,我想添加一个子文件夹。我在excel电子表格中列出了客户端,这不是CSV,有没有简单的方法来创建这些子文件夹?

New-Item -ItemType目录-Path“P:\ Customer Folders \ Customers A到D \

我使用上面的目录创建,但我不想使用此命令900+次。 -recurse似乎没有这样做,因为它给了我一个错误。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

好的,当我在评论中提供代码时,我忘记了括号。以下是适用于您的内容:

foreach($folder in (gci 'P:\customers a thru d' -directory)){
    new-item -ItemType directory -Path ($folder.fullname+"\Helpdesk")
}

我创建了以下内容以进行测试:

C:\ Temp \ customers a thru d \ Microsoft
C:\ Temp \ customers a thru d \ Starbucks
C:\ Temp \ customers a thru d \ boeing

我运行了上面的代码(根据需要修改了路径),并按预期创建了文件夹。如果你想抑制输出,可以将其输出到|Out-Null,但这取决于你。

答案 1 :(得分:0)

Get-ChildItem的目录参数需要Powershell 3.0版。我得到以下内容与v1一起工作。

Get-ChildItem -path C:\Test | #replace C:\Test with your structure
Where-Object { $_.PSIsContainer } |
ForEach-Object {
    #Write-host $_.FullName
    new-item ($_.FullName+"\Folder Name Here") -type directory  
}
相关问题