# Using Select-Object Correctly?

**URL:** <https://forums.powershell.org/t/using-select-object-correctly/18715>\
**Category:** PowerShell Help\
**Created:** [March 2, 2022, 7:09am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715 "2022-03-02T07:09:23Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 2, 2022, 7:09am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/1 "2022-03-02T07:09:23Z")

</div>

Hello,

I’m having a hard time using the `Select-Object` to output only two of four objects available in the output.

For example, the output below shows `Index`, `Name`, `Description` and `Size`. I would like to output only `Index` and `Name`.

Perhaps I’m wrong about these being objects…? If they are, am I just using `Select-Object` incorrectly?

My code looks like this:

```auto
$wrs_local="C:\Mounts\WindowsRepairSource"
dism /Get-WimInfo /WimFile:"$wrs_local\RepairSource.wim"

```

The code results in the following output:

```auto
Deployment Image Servicing and Management tool
Version: 10.0.19041.844

Details for image : C:\Mounts\WindowsRepairSource\RepairSource.wim

Index : 1
Name : Windows 10 Pro for Workstations
Description : Windows 10 Pro for Workstations
Size : 15,074,686,984 bytes

Index : 2
Name : Windows 11 Pro
Description : Windows 11 Pro
Size : 16,198,033,078 bytes

The operation completed successfully.

```

Output repeats for each Index; up to #6 in my case.

I thought I could use `Select-Object` to limit the output to only `Index` and `Name`.

`dism /Get-WimInfo /WimFile:"$wrs_local\RepairSource.wim" | Select-Object Index,Name`

However, when I do, I only get this:

```auto
Index Name
----- ----

```

… followed by a bunch of blank lines with 10 empty spaces each, including one that I think is supposed to contain the following: `The operation completed successfully.`

I was hoping to get something like this:

```auto
Deployment Image Servicing and Management tool
Version: 10.0.19041.844

Details for image : C:\Mounts\WindowsRepairSource\RepairSource.wim

Index : 1
Name : Windows 10 Pro for Workstations

Index : 2
Name : Windows 11 Pro

The operation completed successfully.

```

Or just this:

```auto
Index : 1
Name : Windows 10 Pro for Workstations

Index : 2
Name : Windows 11 Pro

```

I’m using Microsoft’s [Select-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2) for reference.

Any insight would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2022, 7:34am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/2 "2022-03-02T07:34:58Z")

</div>

Since `DISM.exe` is an external tool - not a PowerShell cmdlet you cannot expect it to output its output as PowerShell objects. It is text. 😉  
If you want to limit the output to some particular text you will have to parse it … you may try something like this:

```auto
dism /Get-WimInfo /WimFile:"$wrs_local\RepairSource.wim" | 
    Foreach-Object {
        if ($_ -match 'Index|Name') {
            $_
        }
    }

```

But you should keep in mind - the output is still text/strings. 👆🏽

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 2, 2022, 8:09am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/3 "2022-03-02T08:09:27Z")

</div>

DISM supports a /Format parameter with table/list, but unfortunately this particular command is not compatible with the table option. I’d just parse it like this.

```auto
$wrs_local="C:\Mounts\WindowsRepairSource"

$output = switch -Regex (dism /Get-WimInfo /WimFile:"$wrs_local\RepairSource.wim"){
    '^Index : (.+$)' {
        $ht = [ordered]@{
            Index = $Matches.1
        }
    }

    '^(Name|Description) : (.+$)' {
        $ht.Add($Matches.1,$Matches.2)
    }

    'Size : (.+) bytes$' {
        $ht.Add('Size',"{0:N2} GB" -f ([decimal]($matches.1 -replace '\.|,') / 1GB))
        [PSCustomObject]$ht
    }
}

# output only for your eyes in the console
$output | Format-List

# use the $output variable however you need
$output | Select-Object Index, Name

$output | Export-Csv -Path \path\to\file.csv -NoTypeInformation

```

Edit

I ended up making a function for it. Feel free to use it if you’d like.

```auto
Function Get-WimInformation {
    <#
    .SYNOPSIS
        Extract image information from Wim files
    .DESCRIPTION
        Extract image information from Wim files and outputs easy to use objects
    .EXAMPLE
        PS C:\> Get-WimInformation -Path E:\Mount\RepairSource.wim
    .EXAMPLE
        PS C:\> 'E:\Mount\RepairSource.wim' | Get-WimInformation
    .EXAMPLE
        PS C:\> Get-ChildItem E:\Mount -Filter *.wim | Get-WimInformation
    .INPUTS
        string
    .NOTES
        https://forums.powershell.org/t/using-select-object-correctly/18715
        https://github.com/krzydoug/Tools/blob/master/Get-WimInformation.ps1
    #>

    [cmdletbinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,Position=0,HelpMessage="Enter the full path to the wim file")]
        [Alias("FullName","WimFile")]
        [string]$Path
    )

    begin{
        $dism = Get-ChildItem C:\Windows\System32 -Filter dism.exe | Select-Object -ExpandProperty FullName
    }

    process{
        switch -Regex (&$dism /Get-WimInfo /WimFile:$Path){
            
            '^Details for image : (.+$)' {
                $obj = [PSCustomObject]@{
                    WimFile = $Matches.1
                    Index = ''
                    Name = ''
                    Description = ''
                    Size = ''
                }
            }

            '^(Index|Name|Description) : (.+$)' {
                $obj.($Matches.1) = $Matches.2
            }

            'Size : (.+) bytes$' {
                $obj.Size = "{0:N2} GB" -f ([decimal]($matches.1 -replace '\.|,') / 1GB)
                $obj
                'Index','Name','Description','Size' | ForEach-Object {$obj.$_ = ''}
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 2, 2022, 9:07pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/4 "2022-03-02T21:07:45Z")

</div>

Hello @Olaf,

I ran your script in PowerShell, and it returned the following:

```auto
Index : 1
Name : Windows 10 Pro for Workstations
Index : 2
Name : Windows 11 Pro
Index : 3
Name : Windows 11 Pro for Workstations
Index : 4
Name : Windows Server 2022 Datacenter
Index : 5
Name : Windows Server 2022 Datacenter (Desktop Experience)
Index : 6
Name : Windows 10 Enterprise LTSC 2021

```

Which is exactly what I was looking to achieve.  
I was playing around with where to place the `Out-File` and I was able to get it to work by adding it here, along with my variable:

```auto
$wrs_local="C:\Mounts\WindowsRepairSource"
dism /Get-WimInfo /WimFile:"$wrs_local\RepairSource.wim" | 
    Foreach-Object {
        if ($_ -match 'Index|Name') {
            $_ | Out-File -FilePath "$wrs_local\RepairSource-WinInfo.txt" -Append # Output WIM file contents
        } 
    }

```

This will definitely work for my needs. Now I just need to look more into parsing text in more detail. I use DISM a lot, and always felt it was limited for outputting information that I would like to use later on when I learn how to better use it with an IF for example.

Thanks for doing this…

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 2, 2022, 9:12pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/5 "2022-03-02T21:12:43Z")

</div>

Hello @krzydoug,

I was playing around with your `Function` and trying out the three examples that you provided with it. One thing I noticed was that it’s throwing errors for the part that’s trying to reformat the numbers for the `Size`.

Although I’m not looking to get more than the `Index` and `Name` from the results at this time, I can see a need for this when gathering this information for a variety of wim files. The `Size` will come in handy when tracking images as they grow after being patched/updated over time.

Output example:

```auto
Cannot convert value "17,861,833,565" to type "System.Int32". Error: "Input string was not in a correct format."
At line:49 char:17
+ $obj.Size = "{0:N2} GB" -f ($Matches.1 / 1GB)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger
 
WimFile : C:\Mounts\WindowsRepairSource\RepairSource.wim
Index : 6
Name : Windows 10 Enterprise LTSC 2021
Description : Windows 10 Enterprise LTSC 2021
Size : 

```

I was playing around with trying to remove or comment out the `Description` and `Size` elements, but I didn’t have any luck. I just made it worse. I tried changing this to this:

`Description = ''` to `#Description = ''`  
`Size = ''` to `#Size = ''`  
`(Index|Name|Description)` to `(Index|Name)`  
`'Index','Name','Description','Size'` to `'Index','Name'`

I would also like to comment out the `WimFile` path from the output for now. This is necessary information when running this against multiple WIM files if I can loop this.

I’m very impressed with the Function. It doesn’t look easy, and I’m grateful for the time you took to make it. I hope that one day I’ll be able to create something like this. 👍

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2022, 9:34pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/6 "2022-03-02T21:34:40Z")

</div>

> [@zero269](#):
>
> One thing I noticed was that it’s throwing errors for the part that’s trying to reformat the numbers for the `Size` .

Seems like the output has thousands separators. So you change the line number 49 to this:

```auto
$obj.Size = "{0:N2} GB" -f (($Matches.1 -replace '\.|,') / 1GB)

```

Regardless of that - when you use the function and you don’t want to have all the properties you don’t have to change the function. You simply select what you need like this:

```auto
Get-WimInformation -Path "<Path to a valid WIM file>" |
    Select-Object -Property Index, Description, Size

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 2, 2022, 9:40pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/7 "2022-03-02T21:40:33Z")

</div>

I left the commas in mine. My first thought is maybe that number is larger than an int. I’ll have to check but either way I’ll update the function. Thanks for the feedback guys!

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 2, 2022, 9:46pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/8 "2022-03-02T21:46:31Z")

</div>

It only took a few minutes. You are quite welcome. I’ve updated the function to work for your case and I also edited my original post. If you have several wim files you can simply pipe Get-Childitem output to the function

```auto
Get-ChildItem -Path Some\Folder -Filter *.wim | Get-WimInformation | Select-Object Index, Name

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2022, 10:09pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/9 "2022-03-02T22:09:20Z")

</div>

> [@krzydoug](#):
>
> I’ve updated the function to work for your case and I also edited my original post.

The error message stays the same:

```auto
Cannot convert value "19.074.156" to type "System.Decimal". Error: "Input string was not in a correct format."
At line:49 char:17
+ ... $obj.Size = "{0:N2} GB" -f ([decimal]$Matches.1 / 1GB)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToDecimal

```

… or the v7.2.1 version of the error:

```auto
InvalidArgument:
Line |
  49 | … $obj.Size = "{0:N2} GB" -f ([decimal]$Matches.1 / 1GB)
     | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert value "19.074.156" to type "System.Decimal". Error: "Input string was not in a correct format."

```

Does it really work for you with the thousands separators?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 2, 2022, 11:24pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/10 "2022-03-02T23:24:14Z")

</div>

Yes it does, I’ll update again with your suggestion and show my results later.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 3, 2022, 1:00am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/11 "2022-03-03T01:00:04Z")

</div>

I’ve added your suggestion and here is the same working on my system

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/3/3ccdbc9670528450c8e0bb25af5ec7cea1298f58.png)

Oh and if the number is less than an int it didn’t require anything on my system, hence my suggested code that didn’t work for you guys. “But it works on my system!” LOL

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/c/c281eaf577371e32c92455a9ec40f9e237349664.png)

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 3, 2022, 2:13am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/12 "2022-03-03T02:13:43Z")

</div>

Hello @Olaf,

I applied your change recommendation to @krzydoug’s Function and no errors:  
Change:

`$obj.Size = "{0:N2} GB" -f (($Matches.1 -replace '\.|,') / 1GB)`

Output:

```auto
WimFile : C:\Mounts\WindowsRepairSource\RepairSource.wim
Index : 1
Name : Windows 10 Pro for Workstations
Description : Windows 10 Pro for Workstations
Size : 14.04 GB

```

Plus I was able to filter the `Function` output as you recommended:

```auto
$wrs_wim="C:\Mounts\WindowsRepairSource\RepairSource.wim"
Get-WimInformation -Path "$wrs_wim" | Select-Object -Property Index, Name

```

Results:

```auto
Index Name                                               
----- ----                                               
1 Windows 10 Pro for Workstations                    
2 Windows 11 Pro                                     
3 Windows 11 Pro for Workstations                    
4 Windows Server 2022 Datacenter                     
5 Windows Server 2022 Datacenter (Desktop Experience)
6 Windows 10 Enterprise LTSC 2021

```

I piped my `Out-File` to get those results in a text file:

```auto
$wrs_wim="C:\Mounts\WindowsRepairSource\RepairSource.wim"
Get-WimInformation -Path "$wrs_wim" | Select-Object -Property Index, Name `
| Out-File -FilePath "$wrs_local\RepairSource-WinInfo.txt" -Append # Output WIM file contents

```

I’m loving this new output where it’s side by side. It’s PERFECT! 👍

Thank you **both** very much!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 3, 2022, 8:45am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/13 "2022-03-03T08:45:28Z")

</div>

> [@krzydoug](#):
>
> Oh and if the number is less than an int it didn’t require anything on my system, hence my suggested code that didn’t work for you guys. “But it works on my system!” LOL

It get’s even worse … even though my system is set up to German I still have to use the English thousands separators in PowerShell

 ![ScreenShot_20220303_094057](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/9/900dc95100232e92ff178cbd233ca4cf0932b0ab.png)  
🤔

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 3, 2022, 8:47am UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/14 "2022-03-03T08:47:15Z")

</div>

> [@zero269](#):
>
> I’m loving this new output where it’s side by side

The default output of up to a maximum of 4 properties is always formatted as table. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/using-select-object-correctly/18715/15 "2024-05-16T20:18:14Z")

</div>


