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

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS