Swoogan
Swoogan

Reputation: 5558

Why does PowerShell output change after loading assembly?

I am getting some unusual output from New-Item in a script if I've first loaded an assembly with reflection. Why?

If I run

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
New-Item -Path "temp" -ItemType directory

I get the following odd output from New-Item:

PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Users\swoogan\Desktop\temp
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\Users\swoogan\Desktop
PSChildName       : temp
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : True
Name              : temp
Parent            : Desktop
Exists            : True
Root              : C:\
FullName          : C:\Users\swoogan\Desktop\temp
Extension         :
CreationTime      : 7/28/2014 11:03:10 AM
CreationTimeUtc   : 7/28/2014 3:03:10 PM
LastAccessTime    : 7/28/2014 11:03:10 AM
LastAccessTimeUtc : 7/28/2014 3:03:10 PM
LastWriteTime     : 7/28/2014 11:03:10 AM
LastWriteTimeUtc  : 7/28/2014 3:03:10 PM
Attributes        : Directory
BaseName          : temp
Mode              : d----

The output I am expecting is:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/28/2014  11:03 AM            temp

If I pipe the output of LoadWithPartialName to Out-Null, then I do get the expected output. I would like to understand what is happening here so I can anticipate these discrepancies in the future.

Upvotes: 1

Views: 184

Answers (1)

Eris
Eris

Reputation: 7638

I think I figured this out.

In the original case with the LoadWithPartialName, the output of the script is an array of PSObject since Powershell sends any unhandled object to the pipeline, and the items are of mixed types (0 = AssemblyInfo from Load; 1 = DirectoryInfo from new-item).

If you redirect the LoadWithPartialName to [void] or even save the output in a variable, the output of the script is a single DirectoryInfo object.

PSObject gets formatted differently than DirectoryInfo

Upvotes: 2

Related Questions