3

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?

10
  • 1
    Change Select-Object {[io.path]::GetExtension($_)} to Select-Object -ExpandProperty Extension. Commented May 11, 2018 at 17:52
  • @Bill_Stewart "Property "Extension" cannot be found." In this case, $_ is a string when being piped. Commented May 11, 2018 at 17:54
  • @JacobColvin That doesn't seem to do anything. No output whatsoever; can't even pipe the output to Get-Member Commented May 11, 2018 at 18:00
  • 1
    I was assuming IO.FileInfo input objects (not unreasonable, since you did not specify what you were piping). In this case you would replace your Select-Object with ForEach-Object {[IO.Path]::GetExtension($_) }. Commented May 11, 2018 at 18:11
  • 1
    If you want a complete answer you should show complete code. Or even better provide a minimal reproducible example Commented May 11, 2018 at 18:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Someone down voted you. I'm not certain why, since this is the correct answer. I appreciate the help!
@NexTerren Technically it's still fine to downvote an accepted answer if the quality is bad, or it's a bad solution, etc... However in this case I'm not sure why especially since they didn't leave a comment. Oh well
-1

If you're dealing with strings, I'd suggest just using regex:

$Paths -replace '.*?(\.\w+$)', '$1'

This will return you a [System.String[]] object with extensions in the form of .exe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.