PowerShell: Want to get the length of the string in output

Hi All,

I am typing this but it is not working. Does anyone know what I could be doing wrong.

The command I wrote is:

                     GCI -file  | foreach {$_.name} | sort-object length | format-table name, length

But it is not working. I am expecting a name of the file and length of the string like 8 characters etc. my file is called mystery so it should have 7 as its output of the name, length.

Thank-you

SQL 75

February 10th, 2015 1:45am

Hi ,

your syntex is wrong .

there is no argument for GCI name "-file"

try get-help gci -full

or

get-help gci -example

if you remove -file you will get name of files try

GCI   | foreach {$_.name.length} | sort-object length | format-table name, length

will give length of respected file wich will show by this command

GCI   | foreach {$_.name} | sort-object length | format-table name, length

Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 2:02am

Get-ChildItem supports both  -File and -Directory.

Help will help: https://technet.microsoft.com/library/hh847897(v=wps.630).aspx

Read the first couple of parameters to see.

   GCI -file  | sort-object length | format-table name, length | ft -auto
Seems to be a rasher of bad answers to day.  YOu were just extracting the name property then trying to sort on a property that doesn't exist.

Do the sort first then select the properties.

it helps to test answers before posting.  I know because I get bit by posting without thinking to often.  I have to remember to think

February 10th, 2015 3:06am

-File argument is present in PowerShell 4.0 not in 2.0. So i answred ....

Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 4:15am

-File argument is present in PowerShell 4.0 not in 2.0. So i answred ....

If someone posts with -File I think we should assume they have the version they claim to have.  Besides 2.0 is pretty much obsolete.

File and Directory have been around for a couple of years now.  Since WMF 3.0 about 4 years ago - December of 2011.

February 10th, 2015 4:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics