$Null check fails in PowerShell 3.0, works in 1.0


hello,

newb forum, howdy all.

i have script made while attempting use; identified issue unsure how correct it...

get-content $myfile | foreach-object {get-childitem $_ | {$_.length -eq $null}}


in powershell v1.0 works fine , returns results want, in 3.0 returns nothing... if remove part matches results have $null length, spits out right results, , displays object has $null length...

any way remedy this?  thoughts , opinions appreciated :)

update:

example results when "where" statement excluded:

mode                lastwritetime     length name ----                -------------     ------ ---- d----         9/24/2014   4:12 pm            testa

you using lack of length property directories, basically. reason stopped working in powershell v3 little bit unexpected. in v3, made easier treat objects (or $null) values collections allowing access .length or .count property on doesn't have property. $null values return 0 .length / .count, single objects return 1 (unless have .length or .count property, in case access property), , collections had 1 or other begin with. example:

$directory = get-item $env:systemroot  $directory | get-member # note:  no length property of kind  $directory.length # 1  $null.length # 0

there many other ways search directories, though.  can use new -directory switch on get-childitem (only works in v3 or later), or can filter based on psiscontainer property instead of length, or check type of object (looking system.io.directoryinfo objects), etc.



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...