none
Powershell store file count in folder into a variable RRS feed

  • Question

  • Powershell newb here. I am trying to display the number of files stored in a directory in a Windows Form GUI I created for my script. I used the cmlet below and use a .net count to get the value:

    $totalCount = Write-Host ( dir $path | Measure-Object ).Count

    This does work and it returns the count number on PS command line, but is stored as a null variable. Any ideas would be great.

    Thursday, April 24, 2014 9:41 PM

Answers

  • write-host does not produce output (it writes to the screen only and you cannot redirect it).

    Remove write-host from your command.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by losahsouza Friday, April 25, 2014 12:56 PM
    Thursday, April 24, 2014 9:50 PM
    Moderator

All replies

  • write-host does not produce output (it writes to the screen only and you cannot redirect it).

    Remove write-host from your command.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by losahsouza Friday, April 25, 2014 12:56 PM
    Thursday, April 24, 2014 9:50 PM
    Moderator
  • Or

    $totalCount=(dir $path ).Count


    ¯\_(ツ)_/¯

    Friday, April 25, 2014 12:00 AM