$string.IndexOf(‘.’) inside powershell function returns method invocation failed

Question:


Returns error: Method invocation failed because [System.IO.FileInfo] doesn’t contain a method named ‘IndexOf’.

But calling it outside a function or from the command line works with out issue.

Is this a limitation of powershell or is there some trick to calling string functions inside powershell functions?

Thanks for the help!

Answer:

IndexOf is a method of type string. I suspect that somewhere in ...doing stuff, you’re operating on $file in such a way that it becomes treated as a System.IO.FileInfo.

I suspect this in part because I’ve tried to replicate it in my environment and got it to work:

Are you operating on $file at all, or using it to modify other files? If you are and don’t mind the object being a [System.IO.FileInfo], you can use $file.fullname to get your path string back. This assumes that you haven’t modified it in such a way that it has become a collection of FileInfo objects.

Source:

$string.IndexOf(‘.’) inside powershell function returns method invocation failed by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply