1

I'm trying to make a script to return a CSV list of all the .exe and .dll stored in a folder with their name, size and version.

To do so, I'm using this script :

$path = 'C:\MyFolder'
Get-ChildItem -Path $path * -include *.dll,*.exe -Recurse |
Select-Object @{Name = 'Name'; Expression = {$_.Name}},
              @{Name = 'Size'; Expression = {$_.Length}},
              @{Name = 'Modified'; Expression = {$_.LastWriteTime}},
              @{Name = 'ProductVersion'; Expression = {($_.VersionInfo.ProductVersion.ToString())}} 

# output on screen
$result | Format-Table -AutoSize

# csv file
$result | Export-Csv -Path 'C:\Users\me\Downloads\softwareVersions.csv' -NoTypeInformation

Unfortunately, some of my files don't have any version, so on the screen, I just see an empty value under the "ProductVersion" column, but because of that, I can't generate my CSV, I get the error :

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null

I tried to replace the concerned line (the one with the version I guess) to try to replace the Version by "Not available" if this information is not found, but I'm a beginner in Powershell code, and it doesn't work... Here's what I tried :

@{Name = 'ProductVersion'; Expression = if(!!$_.VersionInfo.ProductVersion) {'Not available'}else{($_.VersionInfo.ProductVersion)}}

I hope that you have all the needed information, as I'm here to ask your help, code masters :-)

Thanks in advance for your time and your answer!!

Jerome

Jey
  • 13
  • 3
  • 1
    It is not because your `ProductVersion` property is empty but because you'r not assigning the result to `$result `. In other words: `$result = Get-ChildItem -Path $path * -include *.dll,*.exe -Recurse | Select-Object ...` – iRon Jun 17 '20 at 06:58
  • Thank you for your answer @iRon! You were true, it works now! For my understanding, could you tell me how could I replace an empty version with "Not available" as like I tried in this code : '@{Name = 'ProductVersion'; Expression = if(!!$_.VersionInfo.ProductVersion) {'Not available'}else{($_.VersionInfo.ProductVersion)}}' Thanks in advance – Jey Jun 17 '20 at 07:12
  • For replacing the empty value with something else you might use an [Inline If](https://stackoverflow.com/a/25682508/1701026) – iRon Jun 17 '20 at 13:32

2 Answers2

0

You've almost done it by your own ;)

Just remove one exclamation mark (!) in your expression. You entered two.

"!!" will negate it twice which is like not negating it.

swbbl
  • 814
  • 1
  • 4
  • 10
  • Hello @pwndsh, thank you for your answer. I just tried my script and remove one of the "!" but then my ProductVersion column remains empty, no matter of the availability of the version or not... I don't see any "Not available" or neither any version. i don't get any error neither! Any idea? thank you – Jey Jun 17 '20 at 11:32
  • That probably means there is a version available, even if it's just nonsense. Please try '-notin' in your expression: if ($_.VersionInfo.ProductVersion -notin $null, '') { 'Not available' } else { $_.VersionInfo.ProductVersio } – swbbl Jun 17 '20 at 14:37
  • PS (sorry 5 minutes ..): That probably means there is a version available, even if it's just nonsense. Please try '-notin' in your expression (hint: '' are two single quotes): if ($_.VersionInfo.ProductVersion -notin $null, '') { 'Not available' } else { $_.VersionInfo.ProductVersion } Just for information: The property you're trying to gather is a .NET class (VERSION). Therefore, the value is not immediately empty or NULL when there is no version defined. Every version is by default '0.0.0.0'. – swbbl Jun 17 '20 at 14:49
0

Both iRon and pwndsh are correct.

You need to capture the result of the whole code block starting with Get-ChildItem in a variable called $result. Then you can either show whatever is in there on screen or save it as proper CSV file.

Also, when testing for Not Something, you can either use one exclamation mark ! or the -not operator. When you double this you are negating the negation, so in this case I would suggest simply testing for if Something, do this, else do that, to stop the confusion:

$path   = 'C:\MyFolder'
$result = Get-ChildItem -Path $path -Filter '*' -Include '*.dll','*.exe' -Recurse |
          Select-Object @{Name = 'Name'; Expression = {$_.Name}},
                        @{Name = 'Size'; Expression = {$_.Length}},
                        @{Name = 'Modified'; Expression = {$_.LastWriteTime}},
                        @{Name = 'ProductVersion'; Expression = {
                            if ($_.VersionInfo.ProductVersion) {
                                $_.VersionInfo.ProductVersion.ToString()
                            }
                            else {
                                'Not available'
                            }}
                       }

# output on screen
$result | Format-Table -AutoSize

# csv file
$result | Export-Csv -Path 'C:\Users\me\Downloads\softwareVersions.csv' -NoTypeInformation
Theo
  • 57,719
  • 8
  • 24
  • 41