Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: GetType causing odd behavior down stream

Tags:

powershell

I was putting together a simple demo script when I observed some odd behavior in Powershell (v3 installed).

$file = ls 'C:\temp' | Where-Object {$_.Extension -eq '.txt'}
$file.FullName

#$file.GetType()

dir 'c:\temp'

Pretty benign right? It runs as expected (assuming you have a c:\temp directory with a txt file in it). However, when you uncomment the $file.GetType() line the dir line that follows it no longer gives a simple directory listing. Instead it will give a detailed list of all items in the directory. I tried piping the GetType to out-null and then the dir works fine again, but I don't seem to be able do to a GetType and list the directory afterwards.

So I'm curious what did calling GetType do that is causing changes in other cmdlets down stream? Can anyone explain the mechanism causing it?

Also, I tried a few other methods and found some cause the issue (like GetAccessControl) and others don't (like GetHashCode). So it's not just by virtue of calling a method, certain methods cause the behavior.

like image 800
Justin Holbrook Avatar asked Apr 07 '26 16:04

Justin Holbrook


1 Answers

It looks to me like it's just a matter of the default formtter getting confused and switching to a format-list display instead of format-table.

If you uncomment the GetType(), and then explicitly pipe the directory list to FT, it's back to normal.

like image 55
mjolinor Avatar answered Apr 11 '26 00:04

mjolinor