1

Using PowerShell I am trying to remove a specific sub-folder from my project using EnvDTE.

The folder I want to remove is:

MyWebProject\Assets\FolderToDelete

And the script I have started with is:

$DTE.Solution.Projects|Where-Object{$_.Name -eq 'MyWebProject'}|Select-Object -Expand ProjectItems

Using a loop, I then traverse ProjectItems to find "FolderToDelete" (of type ProjectItem). I then call ProjectItem.Remove as well as delete it from disk:

$folderToDelete.Remove()
$folderToDeletePath = $folderToDelete.Remove.Properties.Item("FullPath").Value
Remove-Item -path $folderToDeletePath -recurse
$project.Save()

In the Visual Studio solution explorer, I have "Show All Files" turned on and I can see that "FolderToDelete" is removed from the project, but the solution explorer still shows it as being it on disk, but it has actually been deleted (it goes away if I click "refresh" in the solution explorer").

enter image description here

This causes some issues for me as Visual Studio still seems to think it is on disk.

Using EnvDTE, how can I get Visual Studio to refresh and acknowledge this deletion?

row1
  • 5,568
  • 3
  • 46
  • 72

1 Answers1

1

You say you have no PowerShell experience, so, it would be prudent that at a minimum you take a quick PowerShell Free Online course to make sure you understand what you can and cannot do and what does and does not work. Assuming / guessing, just will lead you to serious hair pulling and unnecessary stress / bad experiences.

Getting Started with Microsoft PowerShell

Interested in learning PowerShell? This Microsoft PowerShell course is designed to teach busy IT professionals, admins, and help desk personnel about how to use PowerShell to improve management capabilities, automate redundant tasks, and manage the environment in scale. Through this PowerShell tutorial, you will learn how PowerShell works and how to make PowerShell work for you from experts Jeffrey Snover, the

Course Outline

Getting Started with PowerShell Don’t Fear the Shell The Help System & Getting Connected Extending the Shell Objects for the Admin The Pipeline: Deeper The Power in the Shell - Automation, Remoting, Scripting & Toolmaking

https://mva.microsoft.com/en-US/training-courses/getting-started-with-microsoft-powershell-8276?l=r54IrOWy_2304984382

Or use Youtube

https://www.youtube.com/results?search_query=beginning+powershell

You don't traverse the file system the way you are trying to with what you have posted.

Normal file system traversal is via...

# List the full path of the current directory and all child folders 
(Get-ChildItem -Path 'D:\MyProject' -Directory -Recurse).FullName

# Only get a specific folder
(Get-ChildItem -Path 'D:\MyProject' -Directory -Recurse -Filter 'Assets').FullName

... then using the Remove-Item cmdlet or by directly navigating the via the full UNC and using the Remove-Item cmdlet.

# Get parameters, example, full and Online help for a cmdlet or function

(Get-Command -Name Remove-Item).Parameters
Get-help -Name Remove-Item -Examples
Get-help -Name Remove-Item -Full
Get-help -Name Remove-Item -Online

I get that you are using Visual Studio, but even, As per MS Docs, ProjectItem.Remove Method.. https://learn.microsoft.com/en-us/dotnet/api/envdte.projectitem.remove?view=visualstudiosdk-2017

And the docs, specifically state what and how it is used.

For the ProjectItem object, removes a project item from the project. If ProjectItem.ProjectItems is not null — that is, if there's a subdirectory — then Remove removes all of the project items in that directory and its subdirectories.

There is no recurse method/option on this one, so you have to do this manually.

This is even covered here:

Cleaning up Visual Studio project folders using PowerShell

Over the years I have created a lot of code samples and demo projects using Visual Studios. I keep many of these in a single folder which contains a several subfolders for organizing the code based on the technology being used. As you open, build and close Visual Studio projects a lot of additional files and folders get created.

https://blogs.msdn.microsoft.com/rbrundritt/2014/09/18/cleaning-up-visual-studio-project-folders-using-powershell

If you are going the pure Visual Studio way, here is a post that talks to folder enumeration using DTE, then you can take whatever action on it as needed.

How to get folders under projects?

I am trying to get a list of projects and folders under it. I am able to get the projects and project-items using:

How to get folders under projects?

postanote
  • 15,138
  • 2
  • 14
  • 25
  • Thanks for your detailed reply. I have modified to post to be more about the EnvDTE API instead of PowerShell. Thanks – row1 Feb 02 '18 at 01:05