在PowerShell中使用数组删除文件夹?

时间:2022-01-15 02:57:05

I am new to PowerShell and trying to get used to the strange syntax. I am trying to delete an array of folder paths. This is how I would do it in C#:

我是PowerShell的新手,并试图习惯于奇怪的语法。我想删除一个文件夹路径数组。这就是我在C#中的表现:

string[] folders = { '~/MyFolder/Test123', '~/MyFolder/Test987', '~/MyFolder/Test333' };
foreach (string item in folders) {
  $_.Remove()
}

How would I accomplish this in PowerShell? I found the below script, but I'm not sure how to change to use a pre-defined array. The included parameters actually come from NuGet:

我如何在PowerShell中实现这一目标?我找到了下面的脚本,但我不确定如何更改以使用预定义的数组。包含的参数实际上来自NuGet:

param($installPath, $toolsPath, $package, $project)

$DTE.Solution.Projects|Select-Object -Expand ProjectItems|Where-Object{$_.Name -eq 'Controllers'}|ForEach-Object{$_.Remove()}

How would I incorporate a string array to this instead of the hard coded "Controllers" name.

我如何将字符串数组合并到此而不是硬编码的“控制器”名称。

2 个解决方案

#1


2  

Here's one way to apply a string array to that filter:

这是将字符串数组应用于该过滤器的一种方法:

$ProjectItems = 'Controllers','Robots','Weapons','Bacon'

$DTE.Solution.Projects|
Select-Object -Expand ProjectItems|
Where-Object{$ProjectItems -contains $_.Name}|
ForEach-Object{$_.Remove()}

#2


3  

There are many ways to do it, but this one's pretty simple...

有很多方法可以做到,但这个很简单......

$folders = @("~/MyFolder/Test123", "~/MyFolder/Test987", "~/MyFolder/Test333")
$folders | Remove-Item 

#1


2  

Here's one way to apply a string array to that filter:

这是将字符串数组应用于该过滤器的一种方法:

$ProjectItems = 'Controllers','Robots','Weapons','Bacon'

$DTE.Solution.Projects|
Select-Object -Expand ProjectItems|
Where-Object{$ProjectItems -contains $_.Name}|
ForEach-Object{$_.Remove()}

#2


3  

There are many ways to do it, but this one's pretty simple...

有很多方法可以做到,但这个很简单......

$folders = @("~/MyFolder/Test123", "~/MyFolder/Test987", "~/MyFolder/Test333")
$folders | Remove-Item