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?