Powershell get all filenames associated to a ProjectItem

183 views Asked by At

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

There are 1 answers

1
sodawillow On BEST ANSWER

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