1

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#:

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:

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 Answers 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 
Sign up to request clarification or add additional context in comments.

Comments

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()}

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.