GetChildItem count returns null if there is one file in a folder


hey folks.  i'm absolute newbie ps please forgive me if question ridiculously basic.  i'm using following code see if folder has files in it:

$imagefiles = get-childitem $filespec
if ($imagefiles.count -gt 0) { write-host files found! }
else { write-host folder empty! }

everything working perfectly.  if folder has no files in it, executes else condition.  if has 2 or more files in it, executes code in first set of braces.  where goes wonky if folder has single file in it.  in situation $imagefile.count coming $null instead of 1.  i'm using ps 2.0.

am doing dumb?  tia if can offer assistance!

hi,

in powershell 2 you should use @() operator when getting count property: 

$imagefiles = get-childitem $filespec   if (@($imagefiles).count -gt 0) { write-host files found! }  else { write-host folder empty! }
luck)


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Generating Event ID 91, 44 with CA (In Event Viewer with source as Certsvc)

Group policy default policy cannot be edited because network name not found

Problem when changing update source