Powershell: All new objects not created equal?

I'm working on small demos for PowerShell presentations, and of course one of my own simple demos puzzled me. The purpose of this demonstration is to show various ways of creating objects in powershell. Here is my code:

$file = ls 'C:\temp' | Where-Object {$_.Extension -eq '.txt'}
$file.FullName
$path = 'C:\temp\jdh.txt'
$newfile = New-Object -TypeName System.IO.FileInfo -ArgumentList $path
$newfile.FullName

$file.GetType()
$newfile.GetType()
$file
$newfile

$file | Get-Content | out-null
$newfile | Get-Content | out-null

Simple? Create two FileInfo objects using different methods and read them. The output shows that the files are identical:

C:\temp\jdh.txt
C:\temp\jdh.txt

IsPublic IsSerial Name                                     BaseType                                                                               
-------- -------- ----                                     --------                                                                               
True     True     FileInfo                                 System.IO.FileSystemInfo                                                               
True     True     FileInfo                                 System.IO.FileSystemInfo                                                               

LastWriteTime : 4/10/2013 3:38:29 PM
Length        : 46046
Name          : jdh.txt


LastWriteTime : 4/10/2013 3:38:29 PM
Length        : 46046
Name          : jdh.txt

However, when Get-Content tries to read the $ newfile object, I get the following error:

Get-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the 
input and its properties do not match any of the parameters that take pipeline input.
At line:16 char:12
+ $newfile | Get-Content | out-null
+            ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\temp\jdh.txt:PSObject) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetContentCommand

, , PSObject, , , $newfile FileInfo. , , , - $newfile type [System.IO.FileInfo]. , PSObject FileInfo . , , , $file $newfile , get-content $file? - new-object?

+5
2

, New-Object, , Get-Content - FileInfo.

:

Trace-Command -psHost -Name ParameterBinding {$newFile | gc}
Trace-Command -psHost -Name ParameterBinding {$file | gc}

, , , , Literal , Microsoft.PowerShell.Core\FileSystem::, PsPath. , , Get-Member, , , , , , New-Object -

  • PSChildName
  • PSDrive
  • PSIsContainer
  • PSParentPath
  • PSPath
  • PSProvider

cmdlet Get-Content, , LiteralPath PsPath:

(Get-Command -Name Get-Content).ParameterSets

  Parameter Name: LiteralPath
    ParameterType = System.String[]
    Position = 0
    IsMandatory = True
    IsDynamic = False
    HelpMessage =
    ValueFromPipeline = False
    ValueFromPipelineByPropertyName = True
    ValueFromRemainingArguments = False
    Aliases = {PSPath}
    Attributes =
      System.Management.Automation.AliasAttribute
      System.Management.Automation.ParameterAttribute

New-Object created FileInfo, :

$newFile | Add-Member -MemberType NoteProperty -Name PsPath -Value 'Microsoft.PowerShell.Core\FileSystem::C:\temp\jdh.txt'
+6

, New-Object PowerShell... .

Test-Path $path 

, . , .

Test-Path $path

$stream = [System.IO.StreamWriter] $path
1..10 | % {
      $stream.WriteLine($s)
}
$stream.close()

$newfile = Get-Content $path

$file | Get-Content | out-null
$newfile | Get-Content | out-null

$newfile, . , .

-1

All Articles