JDH
JDH

Reputation: 2822

Powershell: GetType causing odd behavior down stream

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.

Upvotes: 3

Views: 211

Answers (1)

mjolinor
mjolinor

Reputation: 68341

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.

Upvotes: 3

Related Questions