Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

PowerShell: Find the type of objects

📅 2012-Mar-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ powershell, type ⬩ 📚 Archive

The biggest selling point of PowerShell is that it deals with objects instead of mere strings. When putting the pieces of a command pipeline together, one wonders what is the type of objects coming down the pipeline. Finding this out is easy: pipe the output objects to a ForEach-Object loop and print the type of each object.

For example, the type of objects from a text file:

$ Get-Content Foo.txt | ForEach-Object { $_.GetType().FullName }
System.String
System.String
...

Another example, to find the type of objects from a string find:

$ Get-Content Foo.txt | Select-String "blah" | ForEach-Object { $_.GetType().FullName }
Microsoft.PowerShell.Commands.MatchInfo
Microsoft.PowerShell.Commands.MatchInfo
...

Tried with: PowerShell 2.0


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧