Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Selected.System.String to System.String

In Powershell, I'm calling [io.path]::GetExtension(..) as so:

...| Select-Object {[io.path]::GetExtension($_)}

and I get the following output:

[io.path]::GetExtension($_)
---------------------------






.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll

(Note some inputs lack an extension, so their output are empty)

And piping that to "Get-Member" produces:

   TypeName: Selected.System.String

Name                        MemberType   Definition                                
----                        ----------   ----------                                
Equals                      Method       bool Equals(System.Object obj)            
GetHashCode                 Method       int GetHashCode()                         
GetType                     Method       type GetType()                            
ToString                    Method       string ToString()                         
[io.path]::GetExtension($_) NoteProperty System.String [io.path]::GetExtension($_)=

However I want System.String, and not a Selected.System.String, as I want to Group-Object, and I (apparently) cannot group Selected.System.String as it doesn't implement IComparable.

Calling ".ToString()" doesn't throw an error, but it doesn't convert it to a string.

How can I produce a string, or convert the output to a string?

like image 671
Ranger Avatar asked Dec 02 '25 06:12

Ranger


1 Answers

To print your outputs as strings, try this:

'C:\x.ini','C:\y.ini' | % { [io.path]::GetExtension($_) } 

.ini

.ini

TypeName: System.String

In this case % or Foreach-Object will run the command for every input, generating the output you need. Select-Object will actually create a selection object which contains the output. There are many ways to print the contents of this object, but using Foreach-Object is likely the simplest.

like image 182
Jacob Colvin Avatar answered Dec 03 '25 23:12

Jacob Colvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!