Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Where-Object comparison does not execute: Get-ChildItem : The filename, directory name, or volume label syntax is incorrect

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading