What I want to achieve:
Create a list of files, show their name and version and sort alphabetically.
My attempt:
class File_Information
{
[ValidateNotNullOrEmpty()][string]$Name
[ValidateNotNullOrEmpty()][string]$FileVersion
}
$FileList = New-Object System.Collections.Generic.List[File_Information]
foreach ($file in Get-Item("*.dll")){
$C = [File_Information]@{
Name = $file.Name
FileVersion = $file.VersionInfo.FileVersion
}
$FileList = $FileList.Add($C)
}
foreach ($file in Get-Item("*.exe")){
$C = [File_Information]@{
Name = $file.Name
FileVersion = $file.VersionInfo.FileVersion
}
$FileList = $FileList.Add($C)
}
Write-Output $FileList | Sort-Object -Property "Name" | Format-Table
My Powershell version:
Prompt> Get-Host | Select-Object Version
Version
-------
5.1.19041.1320
My problems and/or questions (first is the major question, second and third are optional):
I don't know how to initialise a list of custom objects (I've been looking on the site, but this question only gives the possibility to initialise with an existing value, while I want to initialise to an empty list).
The error I receive is:You cannot call a method on a null-valued expression. At line:... char:... + $FileList = $FileList.Add($C) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Currently I ask for the same thing twice: I have tried
Get-Item(*.dll,*.exe)
,Get-Item(*.dll;*.exe)
andGet-Item(*.dll|*.exe)
but none of them worked.
Is it even possible to search for different patterns?Until now I am struggling just to get
Write-Output $FileList
working. Are the other two commands (Sort-Object -Property "Name"
,Format-Table
) correct?