get-childitem's unexpected behaviour


good day.
$adir=get-childitem -path c:\ps1\oraclebackup\* -include *.lst

if get-childitem finds 1 file $adir.length returns size of found file.

if get-childitem finds more 1 object $adir.length returns size of $adir array.

$adir=@(get-childitem -path c:\ps1\oraclebackup\* -include *.lst) solution.

my question: across board behaviour in powershell, or (hopefully) get-childitem's quirk?

regards
kamil

this expected behaviour powershell

have noticed powershell's cmdlets named in singular?

it's called get-process, not get-processes, get-service, not get-services, etc etc.

the reason because each of these cmdlets sends object @ time pipeline. doesn't need know or care whether single object or multiple.

when receive results, if don't pipe else, powershell check how many objects in pipeline. if it's one, returns is. if more one, create object[] , add objects it.

here's quick example:

function returnonestring {     'hello!' #pushing string pipeline }  function returntwostrings {     'hello ' #pushing string pipeline     'world!' #pushing string pipeline }  (returnonestring).gettype() # system.string  (returntwostrings).gettype() # object[] 

notice on function returntwostrings return type string, it's not array of kind, , specially not object[]. yet, powershell sees 2 objects on pipeline , knows needs group them together, , since objects derive object class, creates object[] place them inside (because returning string + int + list + array + ... )

if of cmdlet you're using @ time, you'll see outputs section tell type of each of individual objects outputs is. whatever powershell decides different thing :)

hope explains somewhat.



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

directory stack

PowerShell List

Dns access list