将值通过管道传递给 New-Item 以在 PowerShell 中创建目录
Piping values to New-Item to create directories in PowerShell
我有一个目录,C:\temp\test\
,包含三个我称为 First.dll、Second.dll、Third.dll 的 DLL。我想创建以每个 DLL 命名的子目录。
这是我目前尝试过的方法:
$dirName = "Tenth"
new-item $dirName -ItemType directory
行得通。它创建了一个名为 "Tenth".
的子目录
这也有效:
(get-childitem -file).BaseName | select $_
它returns:
First
Second
Third
我检查了该命令的输出类型,它告诉我 "select $_" 的类型是 System.String。
现在不起作用的位:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
我重复了 3 次以下错误:
new-item : An item with the specified name C:\temp\test already exists.
At line:1 char:34
+ (get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\temp\test:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
我正在执行命令的当前文件夹是 C:\temp\test\
.
我无法在 Internet 上找到任何类似的示例来告诉我哪里出错了。谁能给我任何指示?干杯。
$_
引用管道中出现的每个项目,因此您需要通过管道传输到 ForEach-Object
才能让您的线路正常工作,如下所示:
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -ItemType directory}
这将在当前 powershell 目录中创建项目,如果要在其他位置创建文件夹,您也可以指定 -Path
。
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -Path C:\MyFolder -ItemType directory}
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
这样就可以了,不需要 ForEach-Object
:
(dir -file).BaseName|ni -name{$_} -ItemType directory -WhatIf
New-Item
接受参数 -Path
进行管道传输,它可以是一个字符串数组。
然后,您可以创建一个包含 属性 Path
的对象,其中包含要创建的所有需要的文件夹
<#
# a simple array as example,
# but it could be the result of any enumerable,
# such as Get-ChildItem and stuff
#>
$FoldersToCreate = @('a', 'b', 'c')
# creates the folders a, b and c at the current working directory
[PSCustomObject]@{ Path = $FoldersToCreate } | New-Item -ItemType Directory
或者,或者:
$FoldersToCreate |
Select-Object @{ name = "Path"; expression = { "c:\testDir$_" } } |
New-Item -ItemType Directory
我有一个目录,C:\temp\test\
,包含三个我称为 First.dll、Second.dll、Third.dll 的 DLL。我想创建以每个 DLL 命名的子目录。
这是我目前尝试过的方法:
$dirName = "Tenth"
new-item $dirName -ItemType directory
行得通。它创建了一个名为 "Tenth".
的子目录这也有效:
(get-childitem -file).BaseName | select $_
它returns:
First
Second
Third
我检查了该命令的输出类型,它告诉我 "select $_" 的类型是 System.String。
现在不起作用的位:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
我重复了 3 次以下错误:
new-item : An item with the specified name C:\temp\test already exists.
At line:1 char:34
+ (get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\temp\test:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
我正在执行命令的当前文件夹是 C:\temp\test\
.
我无法在 Internet 上找到任何类似的示例来告诉我哪里出错了。谁能给我任何指示?干杯。
$_
引用管道中出现的每个项目,因此您需要通过管道传输到 ForEach-Object
才能让您的线路正常工作,如下所示:
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -ItemType directory}
这将在当前 powershell 目录中创建项目,如果要在其他位置创建文件夹,您也可以指定 -Path
。
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -Path C:\MyFolder -ItemType directory}
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
这样就可以了,不需要 ForEach-Object
:
(dir -file).BaseName|ni -name{$_} -ItemType directory -WhatIf
New-Item
接受参数 -Path
进行管道传输,它可以是一个字符串数组。
然后,您可以创建一个包含 属性 Path
的对象,其中包含要创建的所有需要的文件夹
<#
# a simple array as example,
# but it could be the result of any enumerable,
# such as Get-ChildItem and stuff
#>
$FoldersToCreate = @('a', 'b', 'c')
# creates the folders a, b and c at the current working directory
[PSCustomObject]@{ Path = $FoldersToCreate } | New-Item -ItemType Directory
或者,或者:
$FoldersToCreate |
Select-Object @{ name = "Path"; expression = { "c:\testDir$_" } } |
New-Item -ItemType Directory