locked
Powershell: using 2 statements within one { } RRS feed

  • Question

  • Dear Readers,

    I am learning powershell so therefore some elementary questions here..... I have 2 statements below and I have seen some examples where on Microsoft you can combine 2 statements within one brackets but in the case below it doesn't work? Does anyone  know why in this case it is not working....

    I am using only one cmdlet foreach and trying to combine 2 expressions: $.name -and write-host $_$.length

         1.       GCI  -file  | foreach {$_.name} |  foreach { write-host $_ $_.length }    ======This works!

          2.      GCI  -file  | foreach {$_.name  -and   write-host $_ $_.length }   ============= This Doesn't ?

    Thank-you

    SQL 75

    Wednesday, February 11, 2015 5:31 PM

Answers

All replies

  • What are you actually trying to do? If you want to output the sizes of files in the current directory:


    get-childitem -file | select-object Name,Length

    That command will output a list of objects that have a Name property (the name of the file) and the Length property (the file's size).


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 11, 2015 5:43 PM
  • To add to Bill's example this is similar to what you may be thinking.

    GCI  -file  | foreach {write-host $_ $_.length } 

    Although we would likely never do something like that.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, February 11, 2015 5:53 PM
    Wednesday, February 11, 2015 5:53 PM
  • This also gives you a clue:

     GCI  -file  | foreach {$_.name  -and   (write-host $_ $_.length) }


    ¯\_(ツ)_/¯

    Wednesday, February 11, 2015 5:55 PM
  • Thanks Bill and JRV  for your answers,

    I was actually experimenting with PS and wanted to see if It can give me the length of the file name instead of a file size. So foe example if my file is called dog then its length is 3. But my statements above works but the 2nd one with the combination doesn't.

    Thanks

    SQL 75

    Wednesday, February 11, 2015 6:23 PM
  • In PowerShell, script blocks, statement lists, and command blocks are enclosed in curly braces. Within the braces you can include multiple statements by having them on separate lines, or separating them with semicolons.

    Richard Mueller - MVP Directory Services

    Wednesday, February 11, 2015 6:25 PM
  • Thanks Bill and JRV  for your answers,

    I was actually experimenting with PS and wanted to see if It can give me the length of the file name instead of a file size. So foe example if my file is called dog then its length is 3. But my statements above works but the 2nd one with the combination doesn't.

    Thanks

    SQL 75

    Here's how I'd handle something like that:

    Get-ChildItem | 
        Select Name,@{N='FilenameCharCount';E={$_.BaseName.Length}},@{N='FullCharCount';E={$_.Name.Length}}

    See this:

    https://technet.microsoft.com/en-us/library/ff730948.aspx


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Marked as answer by SQL75 Wednesday, February 11, 2015 6:36 PM
    Wednesday, February 11, 2015 6:30 PM