1

I want to get all of the files under a ProjectItem with powershell

enter image description here

In the above I want the paths for the following files:

  • Web.config
  • Web.Debug.config
  • Web.Release.config

I can get the ProjectItem without any problems but I cant work out how to enumerate the FileNames array. All of the elements seem to point to the same thing so I think I must be enumerating the array wrong.

$projectItem.FileNames(0) => Path to web.config
$projectItem.FileNames(1) => Path to web.config

Any number here seems to return the same file path.

How do I get all 3 file paths from the ProjectItem in powershell

To try this out put this in the Package Manager Console of a web project:

@(Get-Project).ProjectItems | Where {$_.Name.StartsWith("Web") } | Select { $_.FileNames(0) }
undefined
  • 33,537
  • 22
  • 129
  • 198
  • From doc: _The index of file names from 1 to FileCount for the project item_. Silly question, have you tried `FileNames(2)`? – sodawillow Jan 03 '17 at 20:55
  • What about using square brackets? `$projectItem.FileNames[0]` – BenH Jan 03 '17 at 20:57
  • 1
    According to [this question](http://stackoverflow.com/questions/14369504/dte-reading-projectitem-filenames-in-powershell) parens should work. I have Visual Studio installed, can you give the steps to have the needed assembly loaded in PS to try and help you? : ) – sodawillow Jan 03 '17 at 20:57
  • @sodawillow yeah i have tried a bunch of other numbers (2, -1 1000) they all return the same thing – undefined Jan 03 '17 at 21:03
  • @Benh $projectItem.FileNames[0] => the name of the property (`string FileNames (short) {get}`) – undefined Jan 03 '17 at 21:04
  • 1
    @sodawillow it should already be loaded by nuget, just open the package manager console and enter the command from the question (View -> Other Windows -> Package Manager Console, Make sure a web project is selected from the default project dropdown) – undefined Jan 03 '17 at 21:06

1 Answers1

1

Got it. Basically you have to expand the ProjectItems collection of the ProjectItem named Web.config. There may be a simpler way, though.

$webconfig = @(Get-Project).ProjectItems |
    Where-Object { $_.Name -eq "Web.config" }

$webconfig.Properties("LocalPath").Value # path for web.config

$webdebugconfig = $webconfig.ProjectItems |
    Where-Object { $_.Name -eq "Web.Debug.config" }

$webdebugconfig.Properties("LocalPath").Value # path for web.debug.config

$webreleaseconfig = $webconfig.ProjectItems |
    Where-Object { $_.Name -eq "Web.Release.config" }

$webreleaseconfig.Properties("LocalPath").Value # path for web.release.config
sodawillow
  • 12,497
  • 4
  • 34
  • 44