Home > Software design >  Where-Object comparison does not execute: Get-ChildItem : The filename, directory name, or volume la
Where-Object comparison does not execute: Get-ChildItem : The filename, directory name, or volume la

Time:01-15

I want to get all *.ddl files with the Name System.ComponentModel.Annotations from all files/folders. I am a PS newbie. The recursive part is copy/paste and should work. I just changed the filtering.

Get-ChildItem Where-Object { [Reflection.AssemblyName]::GetAssemblyName($_.FullName) -eq 'System.ComponentModelAnnotations'} -Recurse |
ForEach-Object {
try {
 $_ | Add-Member NoteProperty FileVersion ($_.VersionInfo.FileVersion)
  $_ | Add-Member NoteProperty AssemblyVersion (
      [Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version
  )
} catch {}
$_
} |
Select-Object Name,FileVersion,AssemblyVersion

The Error I get is:

  Get-ChildItem : The filename, directory name, or volume label syntax is incorrect.
At line:1 char:1
  Get-ChildItem Where-Object { [Reflection.AssemblyName]::GetAssemblyNa ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : ReadError: (C:\Repositories...backend\01.Code:String) [Get-ChildItem], IOException
      FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

What is wrong about the Where-Object comparison?

CodePudding user response:

Get-ChildItem and Where-Object are two separate commands. To compose a pipeline using both, use the pipe symbol | to route the output from one to the other:

Get-ChildItem -Filter *.dll -Recurse |Where-Object { [Reflection.AssemblyName]::GetAssemblyName($_.FullName).Name -eq 'System.ComponentModelAnnotations' } |ForEach-Object { ... }

It might be worth noting that you can skip the ForEach-Object command completely and pipe the output to Add-Member - you just need to add the -PassThru switch to make sure it feeds the modified object back down the pipeline:

Get-ChildItem -Filter *.dll -Recurse |
  Where-Object { [Reflection.AssemblyName]::GetAssemblyName($_.FullName).Name -eq 'System.ComponentModelAnnotations' } |
  Add-Member NoteProperty FileVersion -Value {$_.VersionInfo.FileVersion} -PassThru |
  Add-Member NoteProperty AssemblyVersion -Value {[Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version} -PassThru |
  Select-Object Name,FileVersion,AssemblyVersion
  •  Tags:  
  • Related